curl --dump-header - -H "Content-Type: application/json" -X POST --data '{"latlong": "test"}' http://localhost:8000/geo/api/geolocation/
上述工作正常,但是当我尝试复制下面的ajax中的POST时,我得到500错误。Ajax POST和Django Tastypie
$.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中发现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
检查文档 - 您可能需要在url字符串中设置json选项。 – Marcin
你可以发布'curl'请求标题和jQuery标题之间的差异吗?您可以在Firebug内的控制台标签中看到jQuery标题。 –