API [未处理的承诺拒绝:类型错误:网络请求失败]

ALb*_*wad 5 javascript api

我正在尝试将(Laravel API)与(React 本机应用程序)连接,我在邮递员中测试了 API,它 100% 正常工作,但是当我在本机应用程序中获取时,出现以下错误!

错误

    [Unhandled promise rejection: TypeError: Network request failed]
- node_modules\whatwg-fetch\dist\fetch.umd.js:527:17 in setTimeout$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:135:14 in _callTimer
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:387:16 in callTimers
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:425:19 in __callFunction
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:112:6 in __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:373:10 in __guard
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:111:4 in callFunctionReturnFlushedQueue
* [native code]:null in callFunctionReturnFlushedQueue
Run Code Online (Sandbox Code Playgroud)

在应用程序中,它显示如下: 在此处输入图片说明

有我的 app.js 代码(只有 fetch 函数):

myfun = async()=>{

await fetch('http://127.0.0.1:8000/api/login',{

    method:'POST',
    headers:{
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    body: JSON.stringify({"email":email, "password": passowrd})
}).then(res => res.json())
.then(resData =>{
    alert(resData);
    console.log(resData);
});
}
Run Code Online (Sandbox Code Playgroud)

小智 0

这可能有点晚了,但为了帮助其他人:在您的获取中,您必须添加您的 IPv4 地址(通常以 192 开头)

然后替换

await fetch('http://127.0.0.1:8000/api/login'
Run Code Online (Sandbox Code Playgroud)

await fetch('http://192.xxx.x.xx:8000/api/login'
Run Code Online (Sandbox Code Playgroud)

在 laravel 中,使用同一主机(在终端中)启动您的端口:

php artisan serve --host 192.xxx.x.xx --port="8000"
Run Code Online (Sandbox Code Playgroud)

它对我有用:)