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標題。 –