$ .ajax 400 Bad Request

时间:2014-04-30 18:29:02

标签: jquery ajax backbone.js

目标是通过ajax将对象发送到我为CORS设置的另一台服务器,这里是$.ajax代码段:

$.ajax "#{config.url_root}/register",
  type: "POST"
  data: model.attributes
  dataType: "json"
  success: (data, text, xhr) -> console.log data, text, xhr
  error: (xhr, text, error) -> console.log text, error

正确POSTS数据,但contentTypeapplication/x-www-urlencoded; charset utf-8;当我明确地将contentType的值设置为" application / json; charset = utf-8" , it executes with a request method of OPTIONS ?! and fails to deliver the data ( 404 Not Found`)

1 个答案:

答案 0 :(得分:1)

通过将XMLHttpRequest的Content-Type设置为application/json,您将简单跨域请求转换为非简单跨域请求(read more about CORS here),这意味着您的浏览器必须在您的预期请求之前发送预检(OPTIONS)请求。这样做是为了验证服务器是否允许来自不同来源的客户端发出请求。

您可以设置的唯一“简单”标题是:AcceptAccept-LanguageContent-LanguageLast-Event-IDContent-Type(如果设置为其中一个:application/x-www-form-urlencodedmultipart/form-datatext/plain)。