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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:fetch() 在 react-native (iOS) 上做 GET 而不是 POST
基础教程推荐
- Play 商店的设备兼容性问题 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
