为了账号安全,请及时绑定邮箱和手机立即绑定

未处理的承诺拒绝:错误:网络错误

未处理的承诺拒绝:错误:网络错误

千巷猫影 2022-05-14 15:13:40
我正在尝试从 API 获取数据。我这样做了:const testScreen=()=>{const c =  axios.get("http://localhost:3000/unverifiedProperty/warehouse",{    headers:{        'Authorization':'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN1dGhhcmhpbWFzbmh1OThAZ21haWwuY29tIiwiaWF0IjoxNTgzODY0NjgwfQ.fxclNhIaNkTnINwOinqFRitX_AA7nQCrLtaFBLY99Tc'    }})console.log(c)return(    <View>        <Text>This is test Screen</Text>    </View>)}我想使用获取请求获取数据。在终端中,我得到了这个:  Running application on Himanshu.  Promise {  "_40": 0,  "_55": null,  "_65": 0,  "_72": null,  }[Unhandled promise rejection: Error: Network Error]- node_modules\axios\lib\core\createError.js:15:17 in createError- node_modules\axios\lib\adapters\xhr.js:78:22 in handleError- node_modules\event-target-shim\dist\event-target-shim.js:818:39 in EventTarget.prototype.dispatchEvent- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:574:29 in setReadyState- node_modules\react-native\Libraries\Network\XMLHttpRequest.js:388:25 in __didCompleteResponse- node_modules\react-native\Libraries\vendor\emitter\EventEmitter.js:190:12 in emit- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:436:47 in __callFunction- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:26 in __guard$argument_0- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:110:17 in __guard$argument_0* [native code]:null in callFunctionReturnFlushedQueue我已经确定使用邮递员我没有网络问题。您可以查看此屏幕截图:
查看完整描述

3 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

这是用于从服务器获取数据的示例 axios.get


const c = axios

  .get("http://localhost:3000/unverifiedProperty/warehouse", {

    headers: {

       Authorization: "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN1dGhhcmhpbWFzbmh1OThAZ21haWwuY29tIiwiaWF0IjoxNTgzODY0NjgwfQ.fxclNhIaNkTnINwOinqFRitX_AA7nQCrLtaFBLY99Tc"

    }

  })

  .then(res => res) //Response from the server

  .catch(err => err); 

如果您在 Android 模拟器上进行测试,我认为您的问题在于名称localhost


代替localhost试试这个 IP 地址10.0.2.2


像这样 :


http://10.0.2.2:3000/unverifiedProperty/warehouse


查看完整回答
反对 回复 2022-05-14
?
ITMISS

TA贡献1871条经验 获得超8个赞

有时,如果我们使用 Localhost,通常会出现错误,请尝试使用您计算机的 IP 地址。

如果您是 Linux 或 Mac 用户,您可以通过ifconfig检查

如果您是 windows 用户,您可以使用命令:ipconfig in cmd

然后将 localhost 替换为您的 IP 地址。


查看完整回答
反对 回复 2022-05-14
?
ibeautiful

TA贡献1993条经验 获得超6个赞

首先,缺少功能测试屏幕右括号 (})


其次 axios.get(或任何其他方法)返回一个 promise 。要访问您的 c,您需要执行类似的操作。如果承诺失败,它将被缓存在 .catch 方法中,您可以记录它并查看是否存在问题


const c =  axios.get("http://localhost:3000/unverifiedProperty/warehouse",{

    headers:{

        'Authorization':'Bearer 0000000'

    }

}).then(res=>res)

.catch(err=>err)

这是有关承诺如何工作的链接 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise


查看完整回答
反对 回复 2022-05-14
  • 3 回答
  • 0 关注
  • 431 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号