ReactNative基础---Networking
来源:互联网 发布:淘宝网天猫手机支架 编辑:程序博客网 时间:2024/06/05 13:34
ReactNative基础—Networking
一、简介
几乎所有的应用都需要和服务器端进行交互,以此从服务器端获取各种数据。RN中网络请求简单明了并且支持多种请求方式。下面让我们一起看看官方标准的网络请求方法(fetch)如何使用。
二、基础知识
- fetch方法支持GET和POST请求方式
- 第一个参数输入URL,第二个参数用来自定义HTTP请求,添加自定义的请求头或者POST请求传递的参数
三、应用
- 请求方式
// GET请求fetch(url, { method: 'GET' })// POST请求 formData为表单格式传递的参数fetch(url, { method: 'POST', body: formData})
- 结果处理
// 发送请求!function getMoviesFromApiAsync() { let url = 'https://facebook.github.io/react-native/movies.json'; //传递参数使用FormData对象 let formData = new FormData(); formData.append("username", 'admin'); loginForm.append("password", '123456'); return fetch(url, { method: 'POST', headers: {}, body: formData }) .then((response) => response.json()) .then((responseJson) => { return responseJson.movies; }) .catch((error) => { console.error(error); });}// 调用发送请求并处理服务器返回数据的方式getMoviesFromApiAsync().then((responseJson) => { // TODO handle data}).catch((error) => { // TODO handle error});
- GET请求处理方式同上,只需将method属性改成GET并去掉参数即可
阅读全文
0 0
- ReactNative基础---Networking
- ReactNative 基础
- 【RN】ReactNative基础配置
- ReactNative ListView基础功能
- ReactNative基础组件
- ReactNative基础---JSX
- ReactNative基础---Handling Events
- ReactNative基础---Handling TextInput
- ReactNative布局基础
- ReactNative基础---Components and Props
- ReactNative基础---State and Lifecycle
- ReactNative基础---Style and Flex
- ReactNative基础---Layout and Flexbox
- Networking
- Networking
- Networking
- Networking
- Networking
- cordova热更新插件的使用
- Mysql数据备份、恢复、数据导入、数据导出的方法
- 英语语法结构体系
- 浅谈Json在java中的使用
- 虚拟内存
- ReactNative基础---Networking
- Java API文档
- VPN连接时报741错误
- HttpServletResponse
- Python基础
- Mybatis中Mapper动态开发的注意点
- 【python】tensorflow一元二次函数拟合
- Spark源码的编译过程详细解读(各版本)(博主推荐)
- 时间管理