首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TypeError:网络请求失败

TypeError:网络请求失败
EN

Stack Overflow用户
提问于 2018-01-02 04:54:40
回答 1查看 1.8K关注 0票数 0

我在RN中有以下代码:

代码语言:javascript
运行
复制
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文件:

代码语言:javascript
运行
复制
<?php
  header('Access-Control-Allow-Origin: *');
  $data = json_decode(file_get_contents('php://input'), true);
  print_r($data);
?>

当我运行postToServer时,我会收到以下消息:

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

有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2018-01-02 06:05:52

添加错误的catch方法:

代码语言:javascript
运行
复制
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));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48055748

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档