【发布时间】:2017-12-05 04:14:42
【问题描述】:
我试图将 react 原生的 android 应用程序与 Laravel Api 连接起来。所以我使用一个简单的 Api 来测试请求是否被发送。使用 POSTMAN 时,它正在工作。但是当我使用 android studio 的模拟器时,它会抛出“网络请求失败”的错误。有什么问题?
POSTMAN测试的API返回json:
{"result":"hello world"}
但在原生反应中
这是我的功能
async onRegisterPressed() {
let response= await fetch('http://127.0.0.1:8000/api/register',{
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
name:this.state.name,
email:this.state.email,
password:this.state.password,
password_confirmation:this.state.password_confirmation,
})
});
let res= await response.json();
console.log(res);
}
【问题讨论】:
-
尝试在您的模拟器浏览器上打开 127.0.0.0:8000。有效吗?
-
遇到网络错误时抛出TypeError。模拟器能上网吗?
-
@SagarKhatri 不工作。
-
@D-reaper 是的,互联网已在模拟器中连接
-
@masterhunter 你可以向一个随机网站发出请求,看看你是否得到回应?
标签: laravel react-native