我试图将 React Native android 应用程序与 Laravel Api 连接起来。所以我使用一个简单的 Api 来测试请求是否被发送。使用 POSTMAN 时它正在工作。但是当我使用 android studio 的模拟器时,它会抛出“网络请求失败”的错误。有什么问题?
POSTMAN测试的API返回json:
{"result":"hello world"}
Run Code Online (Sandbox Code Playgroud)
但在反应原生
这是我的功能
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);
}
Run Code Online (Sandbox Code Playgroud)