Home >Backend Development >PHP Tutorial >javascript - jquery uses the post method to request data, how to pass parameters in the header?
I found this method on the Internet:
$.ajax({
<code> //请求类型,这里为POST type: 'POST', //你要请求的api的URL url: url , //是否使用缓存 cache:false, //数据类型,这里我用的是json dataType: "json", //必要的时候需要用JSON.stringify() 将JSON对象转换成字符串 data: JSON.strigify({key:value}), //data: {key:value}, //添加额外的请求头 headers : {'Access-Control-Allow-Origin':'*'}, //请求成功的回调函数 success: function(data){ //函数参数 "data" 为请求成功服务端返回的数据</code>
},
});
But prompt:
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.
Already in the php code Access-Control-Allow- The value of Headers is set to *, but an error is still reported. Is there any solution?
I found this method on the Internet:
$.ajax({
<code> //请求类型,这里为POST type: 'POST', //你要请求的api的URL url: url , //是否使用缓存 cache:false, //数据类型,这里我用的是json dataType: "json", //必要的时候需要用JSON.stringify() 将JSON对象转换成字符串 data: JSON.strigify({key:value}), //data: {key:value}, //添加额外的请求头 headers : {'Access-Control-Allow-Origin':'*'}, //请求成功的回调函数 success: function(data){ //函数参数 "data" 为请求成功服务端返回的数据</code>
},
});
But prompt:
Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.
Already in the php code Access-Control-Allow- The value of Headers is set to *, but an error is still reported. Is there any solution?
First of all, let’s talk about why the preflight
problem occurs. That’s because your request is no longer a “simple request”. In cross-domain issues, some requests that combine specific conditions are often called simple requests, and those that satisfy “simple requests” The conditions for "Request" are as follows:
The request type must be one of GET
, POST
, HEAD
The request header (Header) can only contain:
Accept
Accept Language
Content Language
Last Event ID
Content Type: only accept application/x-www-form-urlencoded, multipart/form-data, text/plain
Your problem is that, obviously, you added a header Access-Control-Allow-Origin
, causing the request to become "not simple". When the request is not simple, the browser will first send a so-called preflight
request for security reasons with the method OPTIONS
For more information about cross-domain, it is recommended to read Building public APIs and CORS
I understand your problem. It should be fine if you remove the extra header