Categories: React Native 教程

React Native:网络

React Native 的一个目标是成为一个游乐场所,在这里我们可以尝试不同的体系结构和疯狂的想法。自从浏览器使用起来不够灵活,我们别无选择,只能去实现整个堆栈。在这个我们并不打算改变什么的地方,我们试图尽可能忠实于浏览器的 APIS。网络协议栈是一个很好的例子。

XMLHttpRequest

XMLHttpRequest API 是在 iOS networking apis 之上实现的。与 web 显著的区别是其安全模式:由于没有 CORS的概念,你可以从互联网上的任一网站上进行阅读。

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.php);
request.send();

请按照 MDN Documentation,一个对 API 进行了完整描述的文档。

作为一个开发人员,你可能不会直接将 XMLHttpRequest 对象作为他的 API,因为这是一个非常繁琐的工作。但事实上,他的实现和与浏览器 APIS 的兼容能够使你使用第三方库,例如,直接来自 npm 的 Parse 和 super-agent

Fetch

Fetch 是一种更好的网络 API,它的工作是通过标准委员会完成,并且已经在火狐浏览器上可以使用。默认情况下在 React Native 上也是可用的。

fetch(https://mywebsite.com/endpoint.php)
  .then((response) => response.text())
  .then((responseText) => {    console.log(responseText);
  })
  .catch((error) => {    console.warn(error);
  });

冒牌SEO

前端开发者,欢迎大家一起沟通和交流。

Share
Published by
冒牌SEO

Recent Posts

在 Chrome 中删除、允许和管理 Cookie

您可以选择删除现有 Cooki…

2 天 ago

自定义指令:聊聊vue中的自定义指令应用法则

今天我们来聊聊vue中的自定义…

1 周 ago

聊聊Vue中@click.stop和@click.prevent

一起来学下聊聊Vue中@cli…

2 周 ago

Nginx 基本操作:启动、停止、重启命令。

我们来学习Nginx基础操作:…

3 周 ago

Vue3:手动清理keep-alive组件缓存的方法

Vue3中手动清理keep-a…

3 周 ago

聊聊React和Vue组件更新的实现及区别

React 和 Vue 都是当…

4 周 ago