【问题标题】:Android React Native Not Finding My localhost Server (Genymotion and Expo)Android React Native 找不到我的本地主机服务器(Genymotion 和 Expo)
【发布时间】:2018-09-09 16:44:13
【问题描述】:

注意此代码适用于 iOS 但不适用于 Android

let data = fetch('http://mylocalapi.test/api/query?resource=article')
.then((response) => {
  console.log("Get Data");
  console.log(response);
  this.setState({articles:JSON.parse(response._bodyInit)});
});

响应返回以下内容。它似乎试图将我重定向到 dnssearch.com,因为它找不到我的规则。我的 Url 是一个实际的测试域。

我将genymotion 用于模拟器并使用Expo 进行测试

12:16:23: Response {
12:16:23:   "_bodyInit": "<html><head><meta http-equiv=\"refresh\" content=\"0;url=http://www.dnsrsearch.com/index.php?origURL=http://whatshapp.test/api/query&bc=\"/></head><body><script type=\"text/javascript\">window.location=\"http://www.dnsrsearch.com/index.php?origURL=\"+escape(window.location)+\"&r=\"+escape(document.referrer)+\"&bc=\";</script></body></html>",
12:16:23:   "_bodyText": "<html><head><meta http-equiv=\"refresh\" content=\"0;url=http://www.dnsrsearch.com/index.php?origURL=http://whatshapp.test/api/query&bc=\"/></head><body><script type=\"text/javascript\">window.location=\"http://www.dnsrsearch.com/index.php?origURL=\"+escape(window.location)+\"&r=\"+escape(document.referrer)+\"&bc=\";</script></body></html>",
12:16:23:   "headers": Headers {
12:16:23:     "map": Object {
12:16:23:       "cache-control": Array [
12:16:23:         "public, max-age=0",
12:16:23:       ],
12:16:23:       "connection": Array [
12:16:23:         "close",
12:16:23:       ],
12:16:23:       "content-type": Array [
12:16:23:         "text/html",
12:16:23:       ],
12:16:23:       "date": Array [
12:16:23:         "Fri, 30 Mar 2018 16:15:49 GMT",
12:16:23:       ],
12:16:23:       "expires": Array [
12:16:23:         "Fri, 30 Mar 2018 16:15:48 GMT",
12:16:23:       ],
12:16:23:       "server": Array [
12:16:23:         "nginx",
12:16:23:       ],
12:16:23:       "transfer-encoding": Array [
12:16:23:         "chunked",
12:16:23:       ],
12:16:23:       "vary": Array [
12:16:23:         "Accept-Encoding",
12:16:23:       ],
12:16:23:     },
12:16:23:   },
12:16:23:   "ok": true,
12:16:23:   "status": 200,
12:16:23:   "statusText": undefined,
12:16:23:   "type": "default",
12:16:23:   "url": "http://whatshapp.test/api/query?resource=article",
12:16:23: }

【问题讨论】:

    标签: reactjs typescript react-native genymotion expo


    【解决方案1】:

    我之前在 ios 工作的地方遇到过这个问题,但在使用 localhost 或 0.0.0.0 时,android 没有。对我有用的解决方案是使用我的内部 ip,如 here 所述

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-05
      • 2019-09-26
      • 1970-01-01
      相关资源
      最近更新 更多