2016-12-31 92 views
1

正如許多人在回答類似的問題建議的成功,我已經編輯info.plist加入我:陣營本地網絡請求在iPhone上失敗,但在iPhone模擬器

<key>NSAllowsArbitraryLoads</key> 
<true/> 

我也曾在Xcode確信任意負載允許: enter image description here

一切工作完全正常的iPhone模擬器,但是當我試圖讓我的iPhone的網絡請求,我得到這個錯誤在遠程調試器:

TypeError: Network request failed 
    at XMLHttpRequest.xhr.onerror (fetch.js:441) 
    at XMLHttpRequest.dispatchEvent (event-target.js:172) 
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:542) 
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:378) 
    at XMLHttpRequest.js:482 
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181) 
    at MessageQueue.__callFunction (MessageQueue.js:236) 
    at MessageQueue.js:108 
    at guard (MessageQueue.js:46) 
    at MessageQueue.callFunctionReturnFlushedQueue (MessageQueue.js:107) 

我已經重新啓動Xcode的次數比我現在可以計算的更多。我從來沒有與任何其他應用程序的這個問題。這裏從請求的代碼,錯誤源於從底部第二catch

loginFinished (error, result) { 
    if (error) { 
    console.log(error) 
    alert("Login failed with error: " + error); 
    } else if (result.isCancelled) { 
    alert("Login was cancelled"); 
    } else { 
    AccessToken.getCurrentAccessToken() 
     .then(response => { 
     console.log('1',response) 
     fetch(`http://localhost:3000/auth`, { 
      method: 'POST', 
      headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'application/json' 
      }, 
      body: JSON.stringify({ 
      fbAccessToken: response.accessToken 
      }) 
     }) 
     .then(response => response.json()) 
     .then(data => { 
      console.log('data: ', data) 
      this.props.setUser(data) 
     }) 
     //Network error comes from this catch 
     .catch(err => console.log(err)) 
     }) 
     .catch(err => console.log(err)) 
    } 
} 

回答

2

的問題是,你正試圖從手機訪問localhost。當您在與服務器相同的計算機上運行iOS模擬器時,可以正常工作,但您的手機需要計算機的主機名或IP地址。這些都是你可以解決這幾個方面:

  • http://<hash>.ngrok.io電腦(ngrok http 3000)和訪問其上運行的網絡隧道像ngrok。由於ngrok可以終止TLS,因此您也可以測試HTTPS網址。
  • 找到您的計算機的主機名並訪問http://<hostname>.local:3000。這需要您的計算機和路由器支持Bonjour。
  • 找到您的計算機的LAN IP地址,並訪問它在http://<ip>:3000
相關問題