gpt4 book ai didi

javascript - 获取请求成功完成,但响应对象为空

转载 作者:行者123 更新时间:2023-12-03 14:28:16 24 4
gpt4 key购买 nike

我在 React 组件的 componentDidMount() 方法中有一个获取请求:

const request = new Request(url, {
mode: 'no-cors',
method: 'get',
headers: {
Accept: 'application/json'
}
});

fetch(request)
.then(function(response) {
console.log('Response: ', response);

return response.text();
})
.then(function(data) {
// _this.setState({ data: data });

console.log('Success: ', data);
console.log('Request succeeded with JSON response', data);
}).catch(function(error) {
console.log('Request failed', error);
});

当组件加载时,我可以看到在控制台中发出请求并返回正确的数据;但是,我的 Response 对象始终为空:

Response { type: "opaque", url: "", redirected: false, status: 0, ok: false, statusText: "", headers: Headers, bodyUsed: false }

response.text() 返回 null,我真的不确定发生了什么。我之前也用过同样的方法,没有出现过这个问题,所以不确定是不是第三方数据源的原因还是什么原因。

有什么想法吗?

最佳答案

我建议您关注问题下方的评论:

sideshowbarker 说

If the reason you’re using mode: 'no-cors' is because the server doesn’t send the Access-Control-Allow-Origin response header in its responses, then mode: 'no-cors' is not the way to fix that. The only way to fix it properly is to either configure the server to send Access-Control-Allow-Origin response header or else change your frontend JavaScript code to make you request through a proxy instead. follow this link...

关于javascript - 获取请求成功完成,但响应对象为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44268722/

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