fetch() doing GET instead of POST on react-native (iOS)(fetch() 在 react-native (iOS) 上做 GET 而不是 POST)
问题描述
我的组件中有以下代码:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!