学习本系列内容须要具有必定 HTML 开发基础,没有基础的朋友能够先转至 HTML快速入门(一) 学习react
本人接触 React Native 时间并非特别长,因此对其中的内容和性质了解可能会有所误差,在学习中若是有错会及时修改内容,也欢迎万能的朋友们批评指出,谢谢ios
文章初版出自简书,若是出现图片或页面显示问题,烦请转至 简书 查看 也但愿喜欢的朋友能够点赞,谢谢web
componentDidMount
方法中建立 Ajex 请求,等到请求成功,再用 this.setState
方法从新渲染UIfetch 目前还不是 W3C 规范,是由 whatag 负责研发。与 Ajax 不一样的是,它的 API 不是事件机制,而是采用目前流行的 Promise(MDN Promise) 方式处理json
格式:axios
fetch(url, init) .then((response) => { // 数据解析方式 }) .then((responseData) => { // 获取到的数据处理 }) .catch((error) => { // 错误处理 }) .done();
init
是一个对象,他里面包含了:
译注:segmentfault
- body:不可传对象,用JSON.stringify({...})也不能够,在jQuery 中会自动将对象封装成 formData 形式,fetch不会。
- mode属性控制师傅跨域,其中 same-origin(同源请求,跨域会报error)、no-cors(默认,能够请求其它域的资源,不能访问response内的属性)和 cros(容许跨域,能够获取第三方数据,必要条件是访问的服务容许跨域访问)。
- 使用 fetch 须要注意浏览器版本,但 React-Native 则不须要考虑。
response
对象能够有以下几种解析方式
fetch(url) .then((response) => response.json()) // json方式解析,若是是text就是 response.text() .then((responseData) => { // 获取到的数据处理 }) .catch((error) => { // 错误处理 }) .done();
方式一:react-native
fetch(url, { method: "POST", headers: { "Content-Type": "application/x-www-form-urlencoded" } body:"key1=value&key2=value…&keyN=value" }) .then((response) => { // 数据解析方式 }) .then((responseData) => { // 获取到的数据处理 }) .catch((error) => { // 错误处理 }) .done();
方式二:跨域
let formData = new FormData(); formData.append("参数", "值"); formData.append("参数", "值"); fetch(url, { method:'POST, headers:{}, body:formData, }).then((response)=>{ if (response.ok) { return response.json(); } }).then((json)=>{ alert(JSON.stringify(json)); }).catch.((error)=>{ console.error(error); })
译注:浏览器
- application/x-www-form-urlencoded: 窗体数据被编码为名称/值对。这是标准的编码格式。 multipart/form-data: 窗体数据被编码为一条消息,页上的每一个控件对应消息中的一个部分。 text/plain: 窗体数据以纯文本形式进行编码,其中不含任何控件或格式字符。
- Fetch 跨域请求的时候默认是不带 cookie 的,若是须要进行设置 credentials:'include'。
console.log(response.headers.get('Content-Type')); ... console.log(response.headers.get('Date'));
译注:缓存
- 下面内容整理自 React-Native 中文网
React Native 中已经内置了 XMLHttpRequest API,一些基于 XMLHttpRequest 封装的第三方库也可使用(如:axios、frisbee)但不能使用 jQuery,由于 jQuery 中还使用了不少浏览器才有而RN中没有的东西
var request = new XMLHttpRequest(); request.onreadystatechange = (e) => { if (request.readyState != 4) { return; } if (request.status === 200) { console.log('success', request.responseText); } else { console.warn('error'); } } request.open('GET', 'https://mywebsite.com/endpoint/'); request.send();
注意:因为安全机制与网页环境有所不一样:在应用中你能够访问任何网站,没有跨域的限制
React Native 还支持 WebSocket
,这种协议能够在单个TCP链接上提供全双工的通讯信道
var ws = new WebSocket('ws://host.com/path'); ws.onopen = () => { // 打开一个链接 ws.send('something'); // 发送一个消息 }; ws.onmessage = (e) => { // 接收到了一个消息 console.log(e.data); }; ws.onerror = (e) => { // 发生了一个错误 console.log(e.message); }; ws.onclose = (e) => { // 链接被关闭了 console.log(e.code, e.reason); };