fetch 写法

fetch("../students.json").then(function(response){
        if(response.status!==200){
            console.log("存在一个问题,状态码为:"+response.status);
            return;
        }
        //检查响应文本
        response.json().then(function(data){
            console.log(data);
        });
}).catch(function(err){
    console.log("Fetch错误:"+err);
})
function status(response){
    if(response.status>=200 && response.status<300){
        return Promise.resolve(response);
    }else{
        return Promise.reject(new Error(response.statusText));
    }
}
function json(response){
    return response.json();
}
fetch("../students.json",{mode:"cors"})//响应类型“cors”,一般为“basic”;
.then(status)//可以链接方法
.then(json) 
.then(function(data){ 
  console.log("请求成功,JSON解析后的响应数据为:",data); })
.then(function(response){ 
  console.log(response.headers.get('Content-Type')); //application/json 
  console.log(response.headers.get('Date')); //Wed, 08 Mar 2017 06:41:44 GMT  
  console.log(response.status); //200  
  console.log(response.statusText); //ok  
  console.log(response.type); //cors  
  console.log(response.url); //http://.../students.json })
.catch(function(err){ 
  console.log("Fetch错误:"+err); 
})
fetch('./video/avegers01.ts', {
      // set header
    })
    .then(function(response) {
      return response.arrayBuffer();
    })
    .then(function(arrayBuffer) {
      // data events signal a new fMP4 segment is ready:
      transmuxer.push(new Uint8Array(arrayBuffer));
    });
原文地址:https://www.cnblogs.com/enych/p/11730637.html