我对提取功能中的请求POST有问题。我使用React和PHP制作REST API,但出现错误Access-Control-Allow-Origin是必需的。我的网络api中有此标头。这是我的PHP代码(开始):
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET,POST,PUT,DELETE");
header("Access-Control-Expose-Headers: access-control-allow-origin");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
header("Content-Type: application/json; charset=UTF-8");
以及在React中:
//method = POST
//body = {"name":"test","body":"test"}
const apiCall = (url, method, body, resolve, reject) => {
fetch(url, {
method: method,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
body: JSON.stringify(body)
}).then(resp => {
if(resp.ok) {
resp.json().then(json => resolve(json));
}
else {
reject(resp);
}
});
}
我尝试与其他服务器和api通信-结果相同。屏幕在Google Chrome浏览器中出现错误:screen
请帮助。
您必须在服务器中添加CORS模块和代理。