TypeError:网络请求android失败

时间:2018-01-02 04:54:41

标签: android reactjs react-native fetch typeerror

我在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:Network   请求失败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)

有人有什么建议吗?

1 个答案:

答案 0 :(得分: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));
相关问题