首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >反应原生Axios在iOS中工作,但不能在安卓中调用

反应原生Axios在iOS中工作,但不能在安卓中调用
EN

Stack Overflow用户
提问于 2020-01-22 03:15:46
回答 3查看 2.7K关注 0票数 0

我似乎不能让下面的Axios调用在Android上工作。它没有命中服务器并显示错误: Error: Network Error。同样的代码也适用于iOS。我需要做一些特殊的事情才能在React Native上启用Android的网络请求吗?

代码语言:javascript
运行
复制
 var url = 'https://***.****.com/api/list';
 axios.get(url, 
           { headers: {'Content-Type': 'application/json'}}
          ).then((response) => {
               console.log("response = ",response.data)
          })
          .catch((error) => {
              console.log("error = ", error)
          });

我在控制台中得到了这个错误:

代码语言:javascript
运行
复制
error = >  Error: Network Error
    at createError (index.bundle?platfor…minify=false:117949)
    at XMLHttpRequest.handleError (index.bundle?platfor…minify=false:117857)
    at XMLHttpRequest.dispatchEvent (index.bundle?platfor…&minify=false:32349)
    at XMLHttpRequest.setReadyState (index.bundle?platfor…&minify=false:31433)
    at XMLHttpRequest.__didCompleteResponse (index.bundle?platfor…&minify=false:31260)
    at index.bundle?platfor…&minify=false:31370
    at RCTDeviceEventEmitter.emit (index.bundle?platfor…e&minify=false:5652)
    at MessageQueue.__callFunction (index.bundle?platfor…e&minify=false:5080)
    at index.bundle?platfor…e&minify=false:4793
    at MessageQueue.__guard (index.bundle?platfor…e&minify=false:5034)
EN

Stack Overflow用户

发布于 2020-02-21 14:16:46

请不要使用"axios“而不是"fetch”。它适用于iOS和安卓系统,下面是获取数据的示例代码。

代码语言:javascript
运行
复制
  Test = async ()=>
    {
      try
      {
     fetch(
          'your url', 
          {
          method: 'POST',
           headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
      },
          body: JSON.stringify({
             firstParam: 'yourValue',
        secondParam: 'yourOtherValue',

          })
        }).then(response => {
          const statusCode = response.status;
          const data = response.json();
          return Promise.all([statusCode, data]);
        })
        .then((res) => {
          if (res[0] == 200 && res[2].request_results.result_header.code == 100)
          {

          }
    }
        }).catch((error) =>
          {
            console.error(error);
          }
        );
        }
      }
      catch (e)
      {

      }

      };

您将在"res“中获取数据,并可以使用for-loop或其他您想要应用的逻辑来提取数据。

票数 -1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59847802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档