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

查看:1242
本文介绍了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加密的请求. 如果您需要从明文网址(以http开头的网址)中获取 您首先需要添加应用传输安全性例外.

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天全站免登陆