阿贾克斯POST和Django的Tastypie [英] Ajax POST and Django Tastypie

查看:163
本文介绍了阿贾克斯POST和Django的Tastypie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 卷曲自卸头 -  -H内容类型:应用程序/ JSON-X POST --data{经纬度:测试}'的http:/ /本地主机:8000 /地理/ API /地理位置/
 

以上工作正常,但是当我尝试复制POST在阿贾克斯下面我得到的500错误。

  $。阿贾克斯({
  键入:POST,
  网址:HTTP://本地主机:8000 /地理/ API /地理位置/',
  数据:{经纬度:测试}
  成功:latlongSaved()
  数据类型:应用/ JSON
  过程数据:假的,
});
 

错误消息是:

  {ERROR_MESSAGE:格式显示应用程序/ x-WWW的形式urlen codeD'没有可用的反序列化方法,请检查您的``formats``和``content_types``你的串行器。 ....}
 

值得注意的,这是跨域和我使用的是通过GIT中发现django-crossdomainxhr-middleware.py:依据

如果我想补充一个内容类型来Ajax调用是这样的:

 的contentType:应用/ JSON
 

我得到这个错误回:

  XMLHtt prequest无法加载的http://本地主机:8000 /地理/ API /地理位置/。请求头字段内容类型不受访问控制 - 允许 - 头不允许的。
请求的URL:http://本地主机:8000 /地理/ API /地理位置/
请求方法:OPTIONS
状态code:200 OK
请求Headersview源
访问控制请求报头:起源,内容类型,接受
访问控制请求-方法:POST
原产地:HTTP://本地主机:3000
响应Headersview源
访问控制 - 允许 - 方法:POST,GET,OPTIONS,PUT,DELETE
访问控制 - 允许 - 产地:*
内容类型:text / html的;字符集= UTF-8
日期:星期二,2011 7时59分49秒格林尼治标准​​时间8月23日
服务器:WSGIServer / 0.1的Python / 2.6.1
 

解决方案

我添加XS_SHARING_ALLOWED_HEADERS中间件和解决的问题。

https://gist.github.com/1164697

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

解决方案

I added XS_SHARING_ALLOWED_HEADERS to the middleware and that solved the problem.

https://gist.github.com/1164697

这篇关于阿贾克斯POST和Django的Tastypie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆