您的位置:首页 > 其它

ajax 触发preflight的配置

2017-07-09 12:31 127 查看

ajax 触发prefight的配置

Here’s sample code for making a CORS request with JQuery. The comments give more details on how certain properties interact with CORS.

$.ajax({

// The 'type' property sets the HTTP method.
// A value of 'PUT' or 'DELETE' will trigger a preflight request.
//如果请求的方法是'put','delete',会触发preflight
type: 'GET',

// The URL to make the request to.
url: 'http://html5rocks-cors.s3-website-us-east-1.amazonaws.com/index.html',

// 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.
//如果content-type的值不是默认的'application/x-www-form-urlencoded',会触发preflight
contentType: 'text/plain',

xhrFields: {
// The 'xhrFields' property sets additional fields on the XMLHttpRequest.
// This can be used to set the 'withCredentials' property.
// Set the value to 'true' if you'd like to pass cookies to the server.
// If this is enabled, your server must respond with the header
// 'Access-Control-Allow-Credentials: true'.
withCredentials: false
},

headers: {
// Set any custom headers here.
// If you set any non-simple headers, your server must include these
// headers in the 'Access-Control-Allow-Headers' response header.
},

success: function() {
// Here's where you handle a successful response.
},

error: function() {
// Here's where you handle an error response.
// Note that if the error was due to a CORS issue,
// this function will still fire, but there won't be any additional
// information about the error.
}
});


原文链接 https://www.html5rocks.com/en/tutorials/cors/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ajax