![](/img/trans.png)
[英]TypeError: Network Request Failed when trying to hit local API from android emulator. React-Native
[英]React-native Android emulator - POST fails with TypeError: Network request failed
我正试图点击VPN上的API。 我确保我的笔记本电脑已连接到VPN - 我可以从Chrome REST客户端成功发出POST登录请求。 此外 - 我的iOS对应应用程序在iOS模拟器上运行正常,运行相同的fetch()代码等。
反应本机版本我正在:
react-native-cli:0.2.0
反应原生:0.22.2
我在登录类中的代码是:
var LOGIN_URL = 'http://<mydomain>/portal/api/v1.0/user/authenticate/';
class DroidLogin extends Component {
...
onLoginPressed() {
this._authenticate();
}
_authenticate() {
fetch(LOGIN_URL + "<username>", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({password:<password>})
})
.then((response) => response.json())
.then((responseData) => {
console.log("Response data token is: " + responseData.token);
})
.catch((error) => {
console.log('We have an error logging in...');
console.warn(error);
})
.done();
}
...
}
我在Chrome调试器中看到的错误:
TypeError: Network request failed
at xhr.onload (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:14679:8)
at XMLHttpRequest._sendLoad (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:57127:1)
at XMLHttpRequest.setReadyState (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:57117:6)
at XMLHttpRequest._didCompleteResponse (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:57012:6)
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:56950:105
at EventEmitter.emit (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:51351:23)
at MessageQueue.__callFunction (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:51547:23)
at http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:51451:8
at guard (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:51405:1)
at MessageQueue.callFunctionReturnFlushedQueue (http://localhost:8081/index.android.bundle?platform=android&dev=true&hot=false:51450:1)
在Android模拟器上运行时需要更改什么? 谢谢!
我第一次遇到这个错误时,我能够通过运行此命令来解决它
adb reverse tcp:8000 tcp:8000
现在请求正常,但它仍然在.catch()
上给我这个错误
[TypeError: Network request failed]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.