如何解决React Fetch的问题-POST请求的问题?

问题描述 投票:0回答:1

我对提取功能中的请求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

请帮助。

php reactjs api header fetch
1个回答
0
投票

您必须在服务器中添加CORS模块和代理。

© www.soinside.com 2019 - 2024. All rights reserved.