axios 出现网络错误并响应本机 [英] Network error with axios and react native

查看:144
本文介绍了axios 出现网络错误并响应本机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用外部托管的 Django python 框架创建了一个 API 端点.我可以从浏览器 (mydomain.com/endpoint/) 访问我的端点并验证没有错误.当我在我的开发机器 (localhost:8000/endpoint/) 上本地运行我的测试 django 服务器时,情况也是如此.当我使用我的本地主机作为端点时,我的 json 数据毫无问题地通过.当我使用我的生产域时,axios 遇到了网络错误,并且它提供的上下文并不多......从调试控制台我得到了这个:

I have created an API endpoint using the Django python framework that I host externally. I can access my endpoint from a browser (mydomain.com/endpoint/) and verify that there is no error. The same is true when I run my test django server on locally on my development machine (localhost:8000/endpoint/). When I use my localhost as an endpoint, my json data comes through without issue. When I use my production domain, axios gets caught up with a network error, and there is not much context that it gives... from the debug console I get this:

Error: Network Error
    at createError (createError.js:16)
    at XMLHttpRequest.handleError (xhr.js:87)
    at XMLHttpRequest.dispatchEvent (event-target.js:172)
    at XMLHttpRequest.setReadyState (XMLHttpRequest.js:554)
    at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:387)
    at XMLHttpRequest.js:493
    at RCTDeviceEventEmitter.emit (EventEmitter.js:181)
    at MessageQueue.__callFunction (MessageQueue.js:353)
    at MessageQueue.js:118
    at MessageQueue.__guardSafe (MessageQueue.js:316)

这是我在 React Native 组件中的 axios 调用:

This is my axios call in my react native component:

    componentDidMount() {
        axios.get('mydomain.com/get/').then(response => {  // localhost:8000/get works
            this.setState({foo:response.data});
        }).catch(error => {
            console.log(error);
        });
    }

推荐答案

貌似在 iOS 中未加密的网络请求默认是被屏蔽的,即 https 会起作用,http不会.

It seems that unencrypted network requests are blocked by default in iOS, i.e. https will work, http will not.

来自文档:

默认情况下,iOS 会阻止任何未使用 SSL 加密的请求.如果您需要从明文 URL(以 http 开头的 URL)中获取您首先需要添加应用传输安全例外.

By default, iOS will block any request that's not encrypted using SSL. If you need to fetch from a cleartext URL (one that begins with http) you will first need to add an App Transport Security exception.

这篇关于axios 出现网络错误并响应本机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆