gpt4 book ai didi

react-native - React Native Fetch 返回网络请求失败

转载 作者:行者123 更新时间:2023-12-02 01:22:06 25 4
gpt4 key购买 nike

我正在使用 React Native 开发一个简单的应用程序。我正在 Genymotion Android 模拟器上测试它。我创建了本地 Web 服务器来监听请求,它运行在 http://localhost:8082/API/。 .我已经测试了 api 并且工作正常。然后我从 index.android.js 发出一个获取请求。

这是来自 React Native 代码的 API 示例请求:

var api = { getUser(){
var url = "http://127.0.0.1:8082/API/";
return fetch(url)
.then((res) => res.json())
.catch(
(error)=>{
console.log('error' + error.message);
throw error;
}
);
}
}
module.exports = api;

这是来自 Api 服务器的代码(使用 flightPHP 构建)

Flight::route('GET /',function(){
try{
$db = new PDO('mysql:host=localhost;port=3307;dbname=testapp', 'root','');
$stmt = $db->prepare("SELECT * FROM user LIMIT 1");
$stmt->execute();
header('Content-type: application/json');
echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));

$db = null;

}catch(Pdoexception $e){
echo $e->getMessage();
}
});

执行此调用后,我收到网络请求失败()。似乎 android 模拟器无法识别 api url。有什么建议吗?先谢谢

最佳答案

我已经解决了。只需将 React 代码中的 API url var url = "http://127.0.0.1:8082/API/ 更改为 var url = "http://local-ip-address :8082/API/要检查您的本地 ip,只需从命令行/cmd 运行 ipconfig

关于react-native - React Native Fetch 返回网络请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39504183/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com