Ajax POST 和 Django Tastypie

发布于 2024-11-30 23:32:52 字数 1509 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半夏半凉 2024-12-07 23:32:52

您在对 curl 的调用中明确声明了您的内容类型,但您没有具体说明 < code>jQuery.ajax() 调用。

更新您的 JavaScript 以准确定义内容类型:

$.ajax({
  type: 'POST',
  url: 'http://localhost:8000/geo/api/geolocation/',
  data: '{"latlong": "test"}',
  success: latlongSaved(),
  dataType: "application/json",
  processData:  false,
  contentType: "application/json"
});

You are explicitly declaring your content type in your call to curl, but you are not being specific on your jQuery.ajax() call.

Update your JavaScript to define exactly what the content type is going to be:

$.ajax({
  type: 'POST',
  url: 'http://localhost:8000/geo/api/geolocation/',
  data: '{"latlong": "test"}',
  success: latlongSaved(),
  dataType: "application/json",
  processData:  false,
  contentType: "application/json"
});
混浊又暗下来 2024-12-07 23:32:52

我将 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

幸福%小乖 2024-12-07 23:32:52

将 XsSharing (https://gist.github.com/1164697) 添加到 settings.py:

MIDDLEWARE_CLASSES = [
    ...,
    'django-crossdomainxhr-middleware.XsSharing'
]

然后使用以下 javascript 进行 ajax 调用:

$.ajax({
  type: 'POST',
  url: 'http://localhost:8000/geo/api/geolocation/',
  data: '{"latlong": "test"}',
  success: latlongSaved(),
  contentType:'application/json',
  dataType: 'application/json',
  processData: false,
});

请注意,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:

MIDDLEWARE_CLASSES = [
    ...,
    'django-crossdomainxhr-middleware.XsSharing'
]

Then use the following javascript to make an ajax call:

$.ajax({
  type: 'POST',
  url: 'http://localhost:8000/geo/api/geolocation/',
  data: '{"latlong": "test"}',
  success: latlongSaved(),
  contentType:'application/json',
  dataType: 'application/json',
  processData: false,
});

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 another POST /geo/api/geolocation request that does the actual creation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文