我正在尝试发送内容类型为json的跨域POST请求。我将服务器头配置为接受原点,标题等。如果我发出GET请求,它会成功,但是,如果我发出POST请求,则预检失败。
以下是网络通信的屏幕截图: http://d.pr/i/JsM
代码:
jq.ajax({
url: url,
type: "POST",
data: dataStr,
dataType: "json",
crossDomain: true,
//processData: false,
contentType: "application/json; charset=utf-8",
xhrFields: {
withCredentials: true
},...
我不知所措的原因是因为GET请求成功,它们之间唯一不同的是GET请求不包括请求中的Access-Control-Request-Headers等标头。
更新:如果我将dataType更改为“text / plain”,则可以正常工作。怎么了?
答案 0 :(得分:0)
请参阅:http://www.html5rocks.com/en/tutorials/cors/。看看来自JQuery"" CORS部分,在代码注释中说:
// The 'contentType' property sets the 'Content-Type' header.
// The JQuery default for this property is
// 'application/x-www-form-urlencoded; charset=UTF-8', which does not trigger
// a preflight. If you set this value to anything other than
// application/x-www-form-urlencoded, multipart/form-data, or text/plain,
// you will trigger a preflight request.
contentType: 'text/plain'