React Native Fetch 请求在 Laravel API 中抛出“网络请求失败”错误

mas*_*ter 3 laravel react-native

我试图将 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)

Mμ.*_*Mμ. 6

我怀疑 127.0.0.1 将指向服务器当前运行的计算机中的模拟器本身。如果您使用的是 AVD,请尝试将 127.0.0.1 替换为 10.0.2.2;如果您使用的是 Genymotion 或使用计算机的实际 IP 地址,请尝试将其替换为 10.0.3.2。