Javascript 跨原点飞行前请求失败(Chrome)
我正在尝试发送内容类型为json的跨域POST请求。我将服务器头配置为接受源文件、头文件等。如果我发出GET请求,它会成功,但是如果我发出POST请求,预飞行会失败 以下是网络通信的屏幕截图: 守则:Javascript 跨原点飞行前请求失败(Chrome),javascript,ajax,google-chrome,cross-domain,Javascript,Ajax,Google Chrome,Cross Domain,我正在尝试发送内容类型为json的跨域POST请求。我将服务器头配置为接受源文件、头文件等。如果我发出GET请求,它会成功,但是如果我发出POST请求,预飞行会失败 以下是网络通信的屏幕截图: 守则: jq.ajax({ url: url, type: "POST", data: dataStr, dataType: "json", crossDomain: true,
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请求不包括请求中的访问控制请求头等等
更新:如果我将数据类型更改为“text/plain”,它将正常工作。怎么回事?看看这个:。看一下“来自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'
您的服务器根本无法正确处理飞行前/选项请求。它的反应是400。你需要修复你的服务器代码。@RayNicholus:啊,是的,我完全绕过了那个。下面是我如何修复它的: