react native TypeError network request failed
2015-12-01 14:35
561 查看
如果使用fetch获取数据,用的是POST方法,注意headers要添加请求头。当请求为GET时不能用body,当为POST时必须包含body,设置头部之后就一切正常了。
[code]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();
[/code]
我在写一个工具的时候,发现自己把自己坑掉了。PC上怎么请求都正常,但是查看日志,包括在浏览器上Debug JS都发现返回的是tomcat 404错误的信息,我郁闷了很久,最后发现是PC上配置了host。而我直接请求时,手机上没有配置host,公网没有那个域名的请求,导致请求找不到。之后我改成直接通过ip请求,在头部中加上Host信息,这样就可以了。
官网也可以查到:https://facebook.github.io/react-native/docs/network.html#fetch
相关文章推荐
- webpack react 打出独立的css文件
- React在Render中使用bind可能导致的问题
- react-native 开发环境搭建
- ReactJS学习笔记(一)
- 是时候和DOM说再见了——《React Native:用JavaScript开发移动应用》读后感
- Netty Reactor模型
- IO设计模式:Reactor和Proactor对比
- react 入门知识
- ReactNativeiOS(二)读书记录 8动手写组件
- ReactiveCocoa概念解释篇
- ReactiveCocoa入门教程——第一部分【转载】
- ReactiveCocoa入门教程——第二部分【转载】
- Reactor 与 Proactor
- ReactiveCocoa入门-part2
- Reactor VS Proactor 模式
- 如何选择前端框架:ANGULAR VS EMBER VS REACT
- ReactiveCocoa入门-part1
- 如何选择前端框架:ANGULAR VS EMBER VS REACT
- 如何选择前端框架:ANGULAR VS EMBER VS REACT
- 初识reactJs 相关