React-native Android模拟器 - POST因TypeError失败:网络请求失败

时间:2016-04-12 16:11:23

标签: android react-native

我正试图点击VPN上的API。我确保我的笔记本电脑已连接到VPN - 我可以从Chrome REST客户端成功发出POST登录请求。此外 - 我的iOS对应应用程序在iOS模拟器上运行正常,运行相同的fetch()代码等。

react-native版本我在:

react-native-cli:0.2.0

react-native: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模拟器上运行时需要更改什么?谢谢!

0 个答案:

没有答案