About fetch

Sometimes mixed feelings, but one hundred die debate, those things very far, as if just yesterday

fetch fetch fetch

fetch is an alternative scheme of XMLHttpRequest, 
in addition to obtaining background data with external ajax we can use fetch them at work, axios instead ajax 

download: npm install WHATWG -fetch the Yarn || --save the Add whatwg- fetch 
introduced: {fetch Import AS fetchPolyfill} from  ' WHATWG-fetch ' 
directly fetchPolyfill can use 
fetch the return value of the object is a promise
componentDidMount () {
     // request componentDidMount data 
    fetchPolyfill first parameter: the request address, the second parameter: CI 
    
    fetchPolyfill ( ' address / request ' , { 
        Method: " request mode " , 
        headers: { ' the Content -Type ' : ' file application / JSON ' }, // request header 
        body: the JSON.stringify ({}) // a POST request needs to be placed in the body and to a string 
    .}) the then (() => { 
        RES .json () 
        // first .then return value is untreated the result set
         // (needs its own to resolve what type) is written above is the processing json json result set 
    }). then (( data) =>{ 
        The console.log (Data) 
        // second .then the return value of the data is retrieved 
    }) 
     
} 
headers issues the HTTP request header: 
Credentials: The omit default, meaning negligible, i.e. there are two cookie without parameter, 
Same - Origin, meaning homologous request with Cookie; 
the include, indicates whether homologous or cross-domain request will bring Cookie 
headers: {credentials: the include} 

must be added to the header parameters which credentials: ' the include ' , 
will as xhr to the cookies as the current request to
fetch is not supported jsonp If you need to use jsonp mounted fetch- jsonp 
the GET request 
componentDidMount () { 
    the let URL = " Portal-API / Common / Channel-info /. 7? inquirykey = & CityId = 31 is & the productId = & lantitude = & longitude = " 
    fetchPolyfill ( URL) // all addresses behind the domain name
     // because it is not necessary to configure get request directly .then 
    .then (RES => res.json ()) 
    .then (Data => { 
      the console.log (Data); // data interface where all 
    }) 
  }
POST请求
需要配置要重启
componentDidMount(){
    let url1 = "/api/product/guessWhatYouLikeV322";
    fetchPolyfill(url1, {
      method: "POST",
      headers:{'Content-Type': 'application/json'},
      body: JSON.stringify({//数据在接口的最下部请求方式最下部
        deviceid: "h5",
        extend: "/api/product/guessWhatYouLikeV322",
        lang: "zh",
        os: "h5",
        param: "{'pageIndex':1}",
"%7B%22pageindex%22%3A1%2C%22isc2clist%22%3A2%2C%22hometabid%22%3A%226039dcc26ae7401191239559335de9ff%22%7D"
上面长串解析后就是{'pageIndex':1}    
        sign: "f779683c8484959386bfef8effbf61de",
        version: "3.1.0"
      })
    })
      .then(res => res.json())
      .then(data => {
        console.log(data)
      })      
}

fetch与axios的区别

 

1、axios在第一个.then中就可以拿到数据,
   fetch在第二个拿,第一个是未处理的结果集
2、axios是一个基于Promise的一个http request的请求方式
        既能在服务端请求也能在客户端请求数据
        服务端: 根据Http进行数据请求
        客户端: 根据XMLHttpRequest进行数据请求
        判断客户是在服务端请求的还是客户端请求的?
           使用属性 Process (只有在服务端才会有node中才会有这个属性)
           这个属性不存在肯定是在客户端
           判断window如果有那肯定是客户端
   fetch不是用XMLHttpRequest进行数据请求的
       特点:符合关注分离,没有将输入、输出和用事件来跟踪的状态混杂在一个对象里
            脱离了XHR,是ES规范里新的实现方式
            fetch没有办法原生监测请求的进度,而XHR可以

 

Guess you like

Origin www.cnblogs.com/home-/p/11582387.html