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

  • 相关阅读:
    蓝桥杯 网络寻路
    ny33 蛇形填数
    集合运算 蓝桥杯 set容器
    蓝桥杯 回形取数
    bam/sam格式说明--转载
    samtools一些文档
    Linux批量更改文件后缀-转载
    GATK--使用转载
    Linux下wget下载整个FTP目录(含子目录)--转载
    CRLF line terminators导致shell脚本报错:command not found --转载
  • 原文地址:https://www.cnblogs.com/meteoric_cry/p/5010004.html
Copyright © 2011-2022 走看看