arn*_*ebr 29 javascript android fetch react-native
我遇到了以下错误.目前我正在使用React Native开发Android应用程序,因此我计划使用fetch为我做一个post请求.
fetch("https://XXreachable-domainXX.de/api/test", {
method: "post",
body: JSON.stringify({
param: 'param',
param1: 'param',
})
}
)
.then((response) = > response.json()
)
.
then((responseData) = > {
ToastAndroid.show(
"Response Body -> " + JSON.stringify(responseData.message), ToastAndroid.SHORT
)
})
.
catch((error) = > {
console.warn(error);
})
;
Run Code Online (Sandbox Code Playgroud)
该应用程序现在抛出一个错误:
TypeError:网络请求失败
当我将代码更改为GET-Request它工作正常时,在浏览器中使用window.alert()作为返回它很酷,并且chrome扩展Postman正确地返回数据.
mp3*_*415 13
在设备上使用Windows OS/PHP内置服务器/ react-native Android进行开发:
ipconfig),例如172.16.0.10fetch使用此URL和正确的端口(fetch('http://172.16.0.10:8000/api/foo))php -S 172.16.0.10:8000 ...这解决了Android手机和本地服务器之间的连接问题.
此React Native的错误相当无用,因此您需要首先获取实际的基础错误.最直接的方法是编写一个只使用相同查询执行相同查询的小型本机程序HttpsURLConnection.
对我来说,实际的错误java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.
有一个众所周知的解决方案:https://developer.android.com/training/articles/security-ssl.html#MissingCa
鉴于浏览器和邮递员对请求没有任何问题,这很可能也是你的情况.检查它运行openssl s_client -connect XXreachable-domainXX.de:443 -showcerts.如果存在证书错误,请先修复它们,这样可以节省您编写本机程序的时间.
编辑:实际上,查看本机反应原生的所有底层android错误的最简单方法就是在终端中运行'adb logcat'
以上答案都没有帮助我。问题是headers:
旧标题:
fetch(API_HOST, {
method: 'POST',
headers: {
Accept: 'application/json'
},
body: JSON.stringify(data),
Run Code Online (Sandbox Code Playgroud)
更新标题:
fetch(config.API_HOST, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json' // I added this line
},
body: JSON.stringify(data),
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
37142 次 |
| 最近记录: |