反应本机TypeError:网络请求失败,使用fetch() [英] React native TypeError: Network request failed with fetch()

查看:580
本文介绍了反应本机TypeError:网络请求失败,使用fetch()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用React native开发一个Android应用程序.通过此提取请求,我得到了错误TypeError: network request failed:

I'm using React native for developing an android application. With this fetch request I'm getting the error TypeError: network request failed:

fetch('https://pixabay.com/api/?key=MY_KEY&q='+ this.props.jsondata.city.name +'&lang=en&image_type=photo&orientation=horizontal&category=travel&safesearch=true')
        .then(response => {
            console.dir(response);
            if (!response.ok) {
                throw Error(data.statusText);
            }
            return response.json();
        })
        .then(json => {
            console.log(json.hits[0].largeImageURL)
            this.setState(() => {
                return {backImage: json.hits[0].largeImageURL};
            });
        })
        .catch(err => {
            console.log('Unable to search image! ' + err);
        });

我已经在线搜索过,但是此问题仅在本地主机地址或http上出现,而不是我的情况.该请求在componentDidMount()函数内部.我在应用程序的另一个地方提出了类似的请求(具有相同的结构),但是那里没有错误.我使用的是真正的android设备,并使用以android本机代码编译的react-native应用程序来测试该应用程序.

I've already searched online but this problem appears only with localhost addresses or http, that is not my case. The request is inside the componentDidMount() function. I've made a similar request (with the same structure) in an another place of the application but there is no error there. I'm using a real android device to testing the app with the react-native app compiled in android native code.

推荐答案

  1. AndroidManifest.xml中添加android:usesCleartextTraffic="true".

从android文件夹中删除所有调试文件夹.

Delete all debug folder from your android folder.

这篇关于反应本机TypeError:网络请求失败,使用fetch()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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