通过Fetch反应本机发布请求会导致网络请求失败

时间:2016-01-02 20:07:24

标签: javascript android react-native fetch

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

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:网络请求失败

当我将代码更改为GET-Request它正常工作时,在浏览器中使用window.alert()作为返回很酷,并且chrome扩展名Postman也正确地返回数据。

9 个答案:

答案 0 :(得分:13)

在设备上使用Windows OS / PHP内置服务器/ react-native Android进行开发:

  • 检查服务器本地IP地址(ipconfig),例如172.16.0.10
  • react-native fetch中的
  • 使用此URL和正确的端口(fetch('http://172.16.0.10:8000/api/foo)
  • 使用此特定IP而不是localhost运行PHP内置服务器:php -S 172.16.0.10:8000 ...
  • 关闭专用网络的Windows防火墙

这解决了Android手机和本地服务器之间的连接问题。

答案 1 :(得分:9)

此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'

答案 2 :(得分:4)

如果您遇到此错误并且确定一切正常您正在运行模拟器,只需关闭您的模拟器并再次启动它。

它现在应该运行。

这通常在您暂停系统一段时间后发生

答案 3 :(得分:1)

以上答案均无济于事。 问题是headers

旧标题:

fetch(API_HOST, {
                method: 'POST',
                headers: {
                    Accept: 'application/json'
                },
                body: JSON.stringify(data),

更新后的标头:

fetch(config.API_HOST, {
                method: 'POST',
                headers: {
                    Accept: 'application/json',
                    'Content-Type': 'application/json'  // I added this line
                },
                body: JSON.stringify(data),

答案 4 :(得分:0)

我在android模拟器上做了同样的主要问题。在iOS上批准info.plist中的域是必要的。为了清楚起见,我试图登录我的.NET Web托管API。

修复是为了确保帖子数据参数化。(我很确定这是一个词)

export const loginUser = ({ userName, password }) => {
const data = `UserName=${userName}&Password=${password}&grant_type=password`
    return (dispatch) => {
        dispatch({ type: LOGIN_USER })

        fetch(URL_LOGIN, {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: data
            // body: {

            //     UserName: userName,
            //     Password: password,
            //     grant_type: 'password'

            // }

        })
            .then((response) => { 
                loginUserSuccess(dispatch, response)
            })
            .catch((response) => {
                loginUserFailed(dispatch, response)
            })
    };
};

答案 5 :(得分:0)

检查以下两个案例

  1. 错误的终点
  2. 互联网连接:真实设备,虚拟设备
  3. 第二个原因已经吃了2个小时。

答案 6 :(得分:0)

如果您在模拟器上遇到此问题,请确保您还在设备上测试它。它很可能不会发生在那里。

只是你知道如果你可以解决它没有什么可担心的。

答案 7 :(得分:0)

由于证书过期,我在Android上遇到此问题。我收到的错误消息是com.android.org.bouncycastle.jce.exception.ExtCertPathValidatorException: Could not validate certificate: Certificate expired at Fri Sep 29 16:33:39 EDT 2017 (compared to Fri Dec 08 14:10:58 EST 2017)

我能够使用digicert.com确认这一点。

不幸的是,我确实需要深入研究React Native代码并调试bundle (index.android.bundle)中的XHR代码,以便找到错误消息和相关URL,因为它在我的一些日志记录中代码,我显然也没有登录到控制台。 :)

我得到了this GitHub issue comment的帮助。

答案 8 :(得分:0)

步骤1> 在AndroidManifest.xml中添加android:usesCleartextTraffic =“ true”行,例如:

//添加此行 ... 第二步> 从您的android文件夹中删除所有调试文件夹。

相关问题