fetch

事实标准,并不存在与ES6规范中,基于Promise实现。

目前项目中对Promise的兼容性尚存在问题,如果在项目中应用fetch,需要引入es6-promisefetch

fis3中可以通过fis3 install es6-promisefis3 install fetch进行安装。

以下提到为了浏览器兼容而引入的fech组件时统一使用'fech组件'代替。

该文档重点针对fetch组件进行详细说明。

相关概念

  • Request、Response、Header、Body:事实标准中暴露在window对象中,但在fetch组件中没有对外暴露接口,项目中不能使用,因此暂不做深入了解。在RN中可以直接使用
  • 返回Promise对象

基本用法

  • get
fetch('/test/content.json').then(function(data){
return data.json();
}).then(function(data){
console.log(data);
}).catch(function(error){
console.log(error);
});
  • post
fetch('/test/content.json', { // url: fetch事实标准中可以通过Request相关api进行设置
method: 'POST',
mode: 'same-origin', // same-origin|no-cors(默认)|cors
credentials: 'include', // omit(默认,不带cookie)|same-origin(同源带cookie)|include(总是带cookie)
headers: { // headers: fetch事实标准中可以通过Header相关api进行设置
'Content-Type': 'application/x-www-form-urlencoded' // default: 'application/json'
},
body: 'a=1&b=2' // body: fetch事实标准中可以通过Body相关api进行设置
}).then(function(res){ res: fetch事实标准中可以通过Response相关api进行设置
return res.json();
}).then(function(data){
console.log(data);
}).catch(function(error){ });

Response相关属性及方法

bodyUsed

  • 标记返回值是否被使用过
  • 这样设计的目的是为了之后兼容基于流的API,让应用一次消费data,这样就允许了JavaScript处理大文件例如视频,并且可以支持实时压缩和编辑
fetch('/test/content.json').then(function(res){
console.log(res.bodyUsed); // false
var data = res.json();
console.log(res.bodyUsed); //true
return data;
}).then(function(data){
console.log(data);
}).catch(function(error){
console.log(error);
});

headers

  • 返回Headers对象,该对象实现了Iterator,可通过for...of遍历
fetch('/test/content.json').then(function(res){
var headers = res.headers;
console.log(headers.get('Content-Type')); // application/json
console.log(headers.has('Content-Type')); // true
console.log(headers.getAll('Content-Type')); // ["application/json"]
for(let key of headers.keys()){
console.log(key); // datelast-modified server accept-ranges etag content-length content-type
}
for(let value of headers.values()){
console.log(value);
}
headers.forEach(function(value, key, arr){
console.log(value); // 对应values()的返回值
console.log(key); // 对应keys()的返回值
});
return res.json();
}).then(function(data){
console.log(data);
}).catch(function(error){
console.log(error);
});

ok

  • 是否正常返回
  • 代表状态码在200-299之间

status

  • 状态码

    • 200 成功

statusText

  • 状态描述

    • 'OK' 成功

type

  • basic:正常的,同域的请求,包含所有的headers。排除Set-CookieSet-Cookie2
  • cors:Response从一个合法的跨域请求获得,一部分header和body可读。
  • error:网络错误。Response的status是0,Headers是空的并且不可写。当Response是从Response.error()中得到时,就是这种类型。
  • opaque: Response从"no-cors"请求了跨域资源。依靠Server端来做限制。

url

arrayBuffer()

  • 返回ArrayBuffer类型的数据的Promise对象

blob()

  • 返回Blob类型的数据的Promise对象

clone()

  • 生成一个Response的克隆
  • body只能被读取一次,但clone方法就可以得到body的一个备份
  • 克隆体仍然具有bodyUsed属性,如果被使用过一次,依然会失效
fetch('/test/content.json').then(function(data){
var d = data.clone();
d.text().then(function(text){
console.log(JSON.parse(text));
});
return data.json();
}).then(function(data){
console.log(data);
}).catch(function(error){
console.log(error);
});

json()

  • 返回JSON类型的数据的Promise对象

text()

  • 返回Text类型的数据的Promise对象

formData()

  • 返回FormData类型的数据的Promise对象

缺陷

  • 无法监控读取进度
  • 无法中断请求

ES6-fetch的更多相关文章

  1. ES6 fetch函数与后台交互

    最近在学习react-native,遇到调用后端接口的问题.看了看官方文档,推荐使用es6的fetch来与后端进行交互,在网上找了一些资料.在这里整理,方便以后查询. 1.RN官方文档中,可使用XML ...

  2. ES6 Fetch API HTTP请求实用指南

    本次将介绍如何使用Fetch API(ES6 +)对REST API的 HTTP请求,还有一些示例提供给大家便于大家理解. 注意:所有示例均在带有箭头功能的 ES6中给出. 当前的Web /移动应用程 ...

  3. ES6 fetch方法封装

    // 请求路径 let url = 'http://jsonplaceholder.typicode.com/users' // 传输数据参数 const dataName = { name: &qu ...

  4. es6 fetch方法请求接口

    fetch(url, { method: 'post', headers: { 'Content-type': 'application/x-www-form-urlencoded; charset= ...

  5. 【原】redux异步操作学习笔记

    摘要: 发觉在学习react的生态链中,react+react-router+webpack+es6+fetch等等这些都基本搞懂的差不多了,可以应用到实战当中,唯独这个redux还不能,学习redu ...

  6. 新一代的json--fetch

    fetch( "http://jsontest.bceapp.com/hi", { method:"POST", mode:"core", ...

  7. Atitit  Uncaught (in promise) SyntaxError Unexpected token < in JSON at position 0

    Atitit  Uncaught (in promise) SyntaxError  Unexpected token < in JSON at position 0  Uncaught (in ...

  8. ES6中Fetch的封装及使用,炒鸡简单~

    之前写过一篇<ajax.axios.fetch之间的详细区别以及优缺点> 戳这里 1.封装 (http.js) class Ajax { get(url) { return new Pro ...

  9. ES6使用fetch请求数据

    ie是完全不支持fetch的. fetch(url,{method:"get/post"}).then(res=>{   }) 如果请求返回的status是200,body是 ...

  10. 用ES6和fetch封装网络请求

    导读: fetch: 这个方法是ES2017中新增的特性,这个特性出来后给人一种传统ajax已死的感觉,其实它的作用是替代浏览器原生的XMLHttpRequest异步请求,我们在日常的开发中,基本不会 ...

随机推荐

  1. (转)CentOS7使用ACL精确控制文件和目录的访问权限

    原文:https://www.linuxidc.com/Linux/2018-01/150111.htm https://blog.csdn.net/maxiaoqiang1/article/deta ...

  2. 下拉菜单;手风琴;九宫格的Jquery的使用实例

    下拉菜单;手风琴;九宫格的Jquery的使用实例 1.下拉菜单 效果如图: 代码如下: <!DOCTYPE html> <html lang="en"> & ...

  3. SpringMVC路径匹配规则AntPathMatcher

    前言 本文是基于Spring Framework 4.3.3分析. 正文 SpringMVC的路径匹配规则是依照Ant的来的. 实际上不只是SpringMVC,整个Spring框架的路径解析都是按照A ...

  4. C++的函数对象优于函数指针地方

    转载自:http://blog.csdn.net/huang_xw/article/details/7934156 在C++编程语言中,有很多功能都与C语言相通,比如指针的应用等等.在这里我们介绍的则 ...

  5. Nodejs学习笔记(二)—事件模块

    一.简介及资料  http://nodejs.org/api/events.html  http://www.infoq.com/cn/articles/tyq-nodejs-event events ...

  6. 尝试用selenium+appium运行一个简单的demo报错:could not get xcode version. /Library/Developer/Info.plist doest not exist on disk

    业余时间抽空搭了个appium+selenium的环境(mac), 在执行第一个脚本的时候遇到个问题纪录下: could not get xcode version. /Library/Develop ...

  7. Fiddler——PC上实现手机的抓包(转载 http://www.jianshu.com/p/13f8a81d7c7c)

    Fiddler是15年初,在千牛中做超级促销插件时,发现没有root的Android机和没有越狱的iPhone无法修改host,因此没办法测试.为了让我这个磨人的PD也能看到,开发推荐了Fiddler ...

  8. django2.1---中间件

    在http请求 到达视图函数之前   和视图函数return之后,django会根据自己的规则在合适的时机执行中间件中相应的方法. Django1.9版本以后中间件的执行流程 1.执行完所有的requ ...

  9. 从mysql中dump数据到本地

    方法一:使用mysqldump命令,如: mysqldump -h10.90.6.237 -uf_insplat2car_r -P3306 -pxxxxxxxxx nbmp tb_tag_log -- ...

  10. Hive 编程指南—笔记

    1. 基础 1.1 Hive 解决问题的背景? 用户如何从一个现有的数据基础架构转移到 Hadoop 上,而这个基础架构是基于传统的关系数据库和 SQL 的? Hive 提供了一个被称为 HQL 的 ...