通过Fetch反应本机发布请求会导致网络请求失败 [英] React Native Post Request via Fetch throws Network Request Failed

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

问题描述

我遇到了以下错误。
目前我正在使用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);
})
    ;

该应用程序现在抛出错误:

The app now throws an 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.

For我的实际错误是 java.security.cert.CertPathValidatorException:找不到证书路径的信任锚。
有一个众所周知的解决方案: 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

考虑到浏览器和Postman对请求没有任何问题,这很可能也是你的情况。要检查它,请运行 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错误只是在终端

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

这篇关于通过Fetch反应本机发布请求会导致网络请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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