zoukankan      html  css  js  c++  java
  • react native TypeError network request failed

    image 

     

    如果使用fetch获取数据,用的是POST方法,注意headers要添加请求头。当请求为GET时不能用body,当为POST时必须包含body,设置头部之后就一切正常了。

     

    fetch("http://xx.xx.xx.xx/login.do?srt=2", {
        method : 'POST',
        body : JSON.stringify({
            SLoginCode : this.state.userName,
            SPasswd : this.state.userPwd,
            randCode : this.state.vertifyCode,
            m : 'login',
            language : 'cn',
            srt : '2'
        }),
        headers : {
            'Accept' : 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;',
            'Content-Type' : 'text/plain;charset=UTF-8',
            'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36',
            'Host' : 'domain.xx.com',
        }
    })
    .then((response) => {
        console.log(response);
    })
    .catch((error) => {
        console.warn(error);
    })
    .done();

    我在写一个工具的时候,发现自己把自己坑掉了。PC上怎么请求都正常,但是查看日志,包括在浏览器上Debug JS都发现返回的是tomcat 404错误的信息,我郁闷了很久,最后发现是PC上配置了host。而我直接请求时,手机上没有配置host,公网没有那个域名的请求,导致请求找不到。之后我改成直接通过ip请求,在头部中加上Host信息,这样就可以了。

     

    官网也可以查到:https://facebook.github.io/react-native/docs/network.html#fetch

  • 相关阅读:
    ICE-3.5.1-错误记录
    windows下qtcreator添加ICE库文件
    LINUX下QT与C语言通过网卡名获取网卡IP与MAC
    Apache部署Django+Vue
    三次握手和四次挥手面试常问
    配置mysql时报错
    nosql的介绍以及和关系型数据库的区别
    redis的基本操作
    在Centos安装redis-孙志奇
    git的使用
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/5010004.html
Copyright © 2011-2022 走看看