使用ApiKeyAuthentication对我的Tastypie资源进行HTTP请求时,当我尝试使用AJAX和Tastypie执行HTTP请求时,出现以下错误:
XMLHttpRequest cannot load http://domain.com/api/v1/item/?format=json&username=popo&api_key=b83d21e2f8bd4952a53d0ce12a2314c0ffa031b1. Request header field Authorization is not allowed by Access-Control-Allow-Headers.
你有解决这个问题的任何想法吗?
以下是来自Chrome的请求头:
Request Headersview source
Accept:*/*
Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:
origin, authorization, access-control-allow-origin, accept, access-control-allow-headers
Access-Control-Request-Method:
GET
以下是来自 Chrome 的响应头:
Response Headersview source
Access-Control-Allow-Headers:
Origin,Content-Type,Accept,Authorization
Access-Control-Allow-Methods:
POST,GET,OPTIONS,PUT,DELETE
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:0
Content-Type:
text/html; charset=utf-8
Date:Fri, 11 May 2012 21:38:35 GMT
Server:nginx
如您所见,它们都有“Authorization”标头,但授权无法正常工作。
这是我使用的Django中间件来编辑响应标头: https://gist.github.com/1164697
编辑:我找到问题了。我试图连接到www.domain.com,但它只接受domain.com。