fetch()在react-native(iOS)上执行GET而不是POST [英] fetch() doing GET instead of POST on react-native (iOS)
问题描述
我的组件中包含以下代码:
I have the following code in my component:
fetch('https://domain.com/api', {
method: 'POST',
headers: {'Accept': 'application/json', 'Content-Type': 'application/json'},
body: JSON.stringify({
key: 'value'
})
}).
then((response) => {
console.log('Done', response);
});
每次请求都是GET(已检查的服务器日志)。我认为这与 CORS
有关(但显然在 react-native
中没有这样的事情)和ATS(但是默认情况下已经关闭,加上我的域名是HTTPS)。我尝试过从浏览器和 curl
开始,它运行良好,所以在服务器配置上没有问题。知道这里发生了什么吗?
And every time the request is a GET (checked server logs). I thought it was something to do with CORS
(but apparently no such thing in react-native
) and ATS (but already turned off by default, plus my domain is HTTPS). I've tried from a browser and from a curl
and it worked perfectly, so a priori no issue on server configuration. Any idea what's going on here?
我正在使用最新的 react-native
版本。
I'm using the latest react-native
version.
推荐答案
进一步挖掘后,这绝对是API + fetch
的问题。我在URL的末尾错过了一个斜杠,并且API发出了301, fetch
无法正确处理。所以我不知道在fetch函数(以及底层机制)中是否有某些东西要修复,但这解决了我的问题:)
After further digging, it was definitely an issue with the API + fetch
. I was missing a slash at the end of the URL and the API issued a 301, that fetch
didn't handle correctly. So I don't know if there is something to fix in the fetch function (and underlying mechanisms) but this fixed my issue :)
这篇关于fetch()在react-native(iOS)上执行GET而不是POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!