簡體   English   中英

React-native Android模擬器 - POST因TypeError失敗:網絡請求失敗

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM