浏览器取消PUT请求

时间:2020-04-15 17:01:54

标签: xmlhttprequest cors axios

我有一个Rails api和一个带有axios的react前端来与api交互。已在rails中启用CORS,但以下请求被浏览器取消,找不到原因。

请求复制为“提取”调用:

fetch("http://localhost:3000/api/v1/profile", 
    {
        "credentials":"include",
        "headers":{
            "accept":"application/json, text/plain, */*",
            "accept-language":"en-GB,en-US;q=0.9,en;q=0.8",
            "authorization":"Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwic2NwIjoiYWNjb3VudCIsImF1ZCI6bnVsbCwiaWF0IjoxNTg2OTY4MjIwLCJleHAiOjE1ODcwNTQ2MjAsImp0aSI6IjhkNTE2YjIzLTA1MGQtNGU2MS04ZWE1LWM3ZGIwMzkxNTg0NCJ9.KuwtVNB5minrOs3lvfNjt7lVQSWNRXdqZsbErb6SrGM",
            "content-type":"application/json",
            "sec-fetch-dest":"empty",
            "sec-fetch-mode":"cors",
            "sec-fetch-site":"same-site"
        },
        "referrer":"http://localhost:3001/profile?",
        "referrerPolicy":"no-referrer-when-downgrade",
        "body":"{\"first_name\":\"testsdasd\",\"last_name\":\"asadeesdfsfs\"}",
        "method":"PUT",
        "mode":"cors"
}); 

这是由于CORS而取消吗?顺便说一句,其他职位要求正在得到解决。

非常感谢。

下面是使用axios的代码。

async updateProfileData(profile) {
        try {
            let axiosResponse = await AxiosClient.instance().put('http://localhost:3000/api/v1/profile', {
                first_name: profile.first_name,
                last_name: profile.last_name
            },{
                headers: {
                    "Content-Type": "application/json"
                }
            });
            return axiosResponse;
        } catch (e) {
            return e.response;
        }
    }

1 个答案:

答案 0 :(得分:-1)

找到答案。

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at ‘http://localhost:3000/api/v1/profile’. (Reason: Credential is not supported if the CORS header ‘Access-Control-Allow-Origin’ is ‘*’).

https://developer.mozilla.org/docs/Web/HTTP/CORS/Errors/CORSNotSupportingCredentials?utm_source=devtools&utm_medium=firefox-cors-errors&utm_campaign=default