Ajax POST 和 Django Tastypie
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"latlong": "test"}' http://localhost:8000/geo/api/geolocation/
上面的工作正常,但是当我尝试在下面的 ajax 中复制 POST 时,出现 500 错误。
$.ajax({
type: 'POST',
url: 'http://localhost:8000/geo/api/geolocation/',
data: '{"latlong": "test"}',
success: latlongSaved(),
dataType: "application/json",
processData: false,
});
错误消息是:
{"error_message": "The format indicated 'application/x-www-form-urlencoded' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." .... }
值得注意的是,这是跨域的,我正在使用通过 git:gist 找到的 django-crossdomainxhr-middleware.py
如果我向 ajax 调用添加内容类型,如下所示:
contentType: "application/json"
我收到此错误:
XMLHttpRequest cannot load http://localhost:8000/geo/api/geolocation/. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Request URL:http://localhost:8000/geo/api/geolocation/
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Access-Control-Request-Headers:Origin, Content-Type, Accept
Access-Control-Request-Method:POST
Origin:http://localhost:3000
Response Headersview source
Access-Control-Allow-Methods:POST,GET,OPTIONS,PUT,DELETE
Access-Control-Allow-Origin:*
Content-Type:text/html; charset=utf-8
Date:Tue, 23 Aug 2011 07:59:49 GMT
Server:WSGIServer/0.1 Python/2.6.1
curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"latlong": "test"}' http://localhost:8000/geo/api/geolocation/
The above works fine but when I try to replicate the POST in the ajax below I get 500 error.
$.ajax({
type: 'POST',
url: 'http://localhost:8000/geo/api/geolocation/',
data: '{"latlong": "test"}',
success: latlongSaved(),
dataType: "application/json",
processData: false,
});
Error message is:
{"error_message": "The format indicated 'application/x-www-form-urlencoded' had no available deserialization method. Please check your ``formats`` and ``content_types`` on your Serializer." .... }
Worth noting this is cross domain and I'm using the django-crossdomainxhr-middleware.py found via git:gist
If I add a content type to the ajax call like this:
contentType: "application/json"
I get this error back:
XMLHttpRequest cannot load http://localhost:8000/geo/api/geolocation/. Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
Request URL:http://localhost:8000/geo/api/geolocation/
Request Method:OPTIONS
Status Code:200 OK
Request Headersview source
Access-Control-Request-Headers:Origin, Content-Type, Accept
Access-Control-Request-Method:POST
Origin:http://localhost:3000
Response Headersview source
Access-Control-Allow-Methods:POST,GET,OPTIONS,PUT,DELETE
Access-Control-Allow-Origin:*
Content-Type:text/html; charset=utf-8
Date:Tue, 23 Aug 2011 07:59:49 GMT
Server:WSGIServer/0.1 Python/2.6.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在对
curl
的调用中明确声明了您的内容类型,但您没有具体说明 < code>jQuery.ajax() 调用。更新您的 JavaScript 以准确定义内容类型:
You are explicitly declaring your content type in your call to
curl
, but you are not being specific on yourjQuery.ajax()
call.Update your JavaScript to define exactly what the content type is going to be:
我将 XS_SHARING_ALLOWED_HEADERS 添加到中间件中,解决了问题。
https://gist.github.com/1164697
I added XS_SHARING_ALLOWED_HEADERS to the middleware and that solved the problem.
https://gist.github.com/1164697
将 XsSharing (https://gist.github.com/1164697) 添加到 settings.py:
然后使用以下 javascript 进行 ajax 调用:
请注意,
data
必须是格式良好的 JSON 字符串,否则 jQuery 将默默地忽略 ajax 调用并且不执行任何操作。幕后的情况是,ajax 调用将首先发送
OPTIONS /geo/api/geolocation/
。由于响应标头是由 XsSharing 中间件修改的,因此 jQuery 将发出另一个进行实际创建的POST /geo/api/geolocation
请求。Add XsSharing (https://gist.github.com/1164697) to settings.py:
Then use the following javascript to make an ajax call:
Notice that
data
must be a well-formed JSON string, otherwise jQuery will silently ignore the ajax call and do nothing.What's behind the scenes is that the ajax call will firstly send out
OPTIONS /geo/api/geolocation/
. Because the response header is modified by XsSharing middleware, jQuery will issue anotherPOST /geo/api/geolocation
request that does the actual creation.