gpt4 book ai didi

即使状态为 200,Reactjs redux : Fetch PUT method sending only an OPTIONS method,

转载 作者:行者123 更新时间:2023-12-03 13:24:44 27 4
gpt4 key购买 nike

我发送如下 PUT 方法(使用 chrome 检查):

function checkStatus(response) {
if (response.status >= 200 && response.status < 300) {
console.log("status: ", response.statusText);
return response
} else {
var error = new Error(response.statusText)
error.response = response
throw error
}
}

import fetch from 'isomorphic-fetch'
return dispatch => {
dispatch(requestPosts(data));
return fetch('/trendings', {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Authorization: Config.token
},
body: JSON.stringify(data),
})
.then(checkStatus)
.then(reponse => {
console.log('request succeeded with JSON response', data);
dispatch(successSent("The output list has been successfully sent!"));
}).catch(err => {
console.log('request failed', err);
dispatch(failSent("Error on sending request: " + err));
});

问题是它只返回状态为 200 的 OPTIONS 方法。

问题是它应该遵循 PUT 方法。此外,它应该返回错误,因为 API 端点尚未准备好。

我错过了什么吗?

最佳答案

您需要添加 credentials: 'same-origin' 并从 header 列表中删除 'Content-Type' : 'application.json',因为 isomorphic-fetch doesn't seem to handle it well以避免此问题,否则第一个 isomorphic-fetch 将仅触发 OPTION 请求来获取有关服务器的信息(这是跨源请求的默认浏览器行为):

import fetch from 'isomorphic-fetch'
return dispatch => {
dispatch(requestPosts(data));
return fetch('/trendings', {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Authorization': Config.token
},
credentials: 'same-origin', // you need to add this line
body: JSON.stringify(data),
})
.then(checkStatus)
.then(reponse => {
console.log('request succeeded with JSON response', data);
dispatch(successSent("The output list has been successfully sent!"));
}).catch(err => {
console.log('request failed', err);
dispatch(failSent("Error on sending request: " + err));
});

关于即使状态为 200,Reactjs redux : Fetch PUT method sending only an OPTIONS method,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37198774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com