通过 Fetch React Native Post Request 引发网络请求失败 [英] React Native Post Request via Fetch throws Network Request Failed

查看:28
本文介绍了通过 Fetch React Native Post Request 引发网络请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下错误.目前我正在使用 React Native 开发一个 Android 应用程序,因此我计划使用 fetch 为我做一个发布请求.

I´ve came across the following error. At the moment I developing an Android App with React Native therefore I´m planning to use fetch for doing a post request for me.

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);
})
    ;

应用现在抛出错误:

TypeError:网络请求失败

TypeError: Network request failed

当我将代码更改为 GET-Request 时,它工作正常,在带有 window.alert() 作为返回的浏览器中,它很酷,而且 Chrome 扩展 Postman 也可以正确返回数据.

When I change the code to a GET-Request it´s working fine, in the browser with a window.alert() as a return it´s cool and also the chrome extension Postman returns data correctly.

推荐答案

这个 React Native 的错误比较没用,所以你需要先获取实际的底层错误.最直接的方法是编写一个小型本地程序,该程序将使用 HttpsURLConnection 执行相同的查询.

This React Native's error is rather useless, so you need to get the actual underlying error first. The most straightforward way is to write a small native program that would just perform the same query using HttpsURLConnection.

对我来说,实际错误是 java.security.cert.CertPathValidatorException: Trust anchor for certificate path not found.它有一个众所周知的解决方案:https://developer.android.com/training/articles/security-ssl.html#MissingCa

For me the actual error was java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. which has a well known solution: https://developer.android.com/training/articles/security-ssl.html#MissingCa

这也很可能是您的情况,因为浏览器和邮递员对请求没有问题.要检查它,请运行 openssl s_client -connect XXreachable-domainXX.de:443 -showcerts.如果有证书错误,请先修复它们,这样可以节省您编写本机程序的时间.

This is quite likely your case also, given that the browsers and Postman have no problem with the request. To check it run openssl s_client -connect XXreachable-domainXX.de:443 -showcerts. If there are certificate errors, fix them first, it could spare you time writing the native program.

实际上,查看所有底层 android 错误的最简单方法是在终端中运行adb logcat"

actually the easiest way to see all underlying android errors for react native is simply running 'adb logcat' in terminal

这篇关于通过 Fetch React Native Post Request 引发网络请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆