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

相关问题