1

我有一個React Native應用程序,只需登錄並隨後使用返回的access_token獲取數據。我試圖通過連接到localHost上運行的本地託管的.NET API來調試錯誤:50968。這返回無法將android React Native應用程序連接到localHost .NET api

TypeError: Network request failed 

我已經在manifestxt.xml中啓用了Internet權限。 大多數谷歌搜索一直在說我需要改變localHost:50968到我的IP地址。這是迄今爲止使用'192.168.0.2:50968/token最接近的,但下面是響應。

{ type: 'default', 
status: 400, 
ok: false, 
statusText: undefined, 
headers: { map: { 'content-length': [ '334' 
     ], 
connection: [ 'close' 
     ], 
date: [ 'Thu, 
      10 Aug201703: 19: 14 GMT' 
     ], 
server: [ 'Microsoft-HTTPAPI/2.0' 
     ], 
'content-type': [ 'text/html; charset=us-ascii' 
     ] 
    } 
}, url: 'http: //192.168.0.2:50968/token', 
_bodyInit: 
'<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">\r\n 
<HTML> 

<HEAD> 
    <TITLE>Bad Request</TITLE>\r\n 
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"> 
</HEAD>\r\n 

<BODY> 
    <h2>Bad Request - Invalid Hostname</h2>\r\n 
    <hr> 
    <p>HTTP Error 400. The request hostname is invalid.</p>\r\n 
</BODY> 

</HTML>\r\n', _bodyText: ' 
<!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"> \r\n 
<HTML> 

<HEAD> 
    <TITLE>Bad Request 
    </TITLE>\r\n 
    <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"> 
</HEAD>\r\n 

<BODY> 
    <h2>Bad Request - Invalid Hostname</h2>\r\n 
    <hr> 
    <p>HTTP Error 400. The request hostname is invalid.</p>\r\n 
</BODY> 

</HTML>\r\n'} 

我在Visual Studio中有一個斷點等待請求,但我不能爲我的生活得到這個工作。 我的提取代碼如下。

Iv將這些作爲URL_LOGIN嘗試使用,但都無效。 'http://localHost:50968/token'; 'http://192.168.0.2:50968/token';

fetch(URL_LOGIN, { 
     method: 'POST', 
     headers: { 
      'Accept': 'application/json', 
      'Content-Type': 'application/json', 
      //application/x-www-form-urlencoded 
     }, 
     body: data 
    }).then((response) => { 
     var jsonResponse = JSON.parse(response._bodyInit); 
     try { 
      AsyncStorage.setItem('token', jsonResponse.access_token); 
      loginUserSuccess(dispatch, response) 
     } catch (error) { 
      console.log(`Error while saving to Async Storage ${error.message}`) 
      loginUserFailed(dispatch, error) 
     } 
    }).catch(response => { 
     loginUserFailed(dispatch, response) 
    }); 

任何幫助將非常感激。

回答

1
+0

時,我得到的迴應與使用第二個鏈接時相同,如果您遇到此問題,則爲生命保護程序。由Keyoti稱爲輸送機。謝謝您的幫助! – tnyN

0

使用10.0.2.2代替localhost

http://10.0.2.2:50968/token

Android上的本地主機映射到仿真器,而不是你的機器。

希望這有助於

+0

是的,我試過這個。當我使用http:// :50968/token – tlagreca

0

在響應你張貼,我注意到它說url: 'http: //192.168.0.2:50968/token',與http://之間的空間。你確定你沒有錯誤地格式化你的網址在URL_LOGIN

相關問題