TypeError:网络请求android失败

问题描述 投票:0回答:1

我在RN中有以下代码:

postToServer(){
  const requestBody = 'pin=1&status=false';
  return fetch('https://192.168.10.200/writeStatus.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: requestBody
  }).then((response) => response.json())
        .then((responseJson) => {
          console.log(responseJson);
        });
}

我也有以下php文件:

<?php
  header('Access-Control-Allow-Origin: *');
  $data = json_decode(file_get_contents('php://input'), true);
  print_r($data);
?>

当我运行postToServer时,我收到:

可能未处理的Promise拒绝(id:0):TypeError:网络请求失败TypeError:XMLHttpRequest.xhr.onerror(http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\ sandbox \ MojDom1 \ node_modules \ expo \ tools \ hashAssetFiles:7854:16)的XMLHttpRequest.dispatchEvent网络请求失败(http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\ sandbox \ MojDom1 \ node_modules \ expo \ tools \ hashAssetFiles:12942:35)...(在这里留下一些行 - 我对链接有限制)在MessageQueue.callFunctionReturnFlushedQueue(http://192.168.10.248:19001/node_modules/expo/AppEntry.bundle?platform=android&dev=true&minify=false&hot=false&assetPlugin=P:\ sandbox \ MojDom1 \ node_modules \ expo) \ TOOLS \ hashAssetFiles:2122:12)

有人有什么建议吗?

android reactjs react-native fetch typeerror
1个回答
0
投票

为错误添加catch方法:

postToServer(){
  const requestBody = 'pin=1&status=false';
  return fetch('https://192.168.10.200/writeStatus.php', {
    method: 'POST',
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    body: requestBody
  }).then((response) => console.log(response))
}).catch(e => console.log(e));
© www.soinside.com 2019 - 2024. All rights reserved.