SUMMARY fetch request mode and

fetch

Fetch API provides a JavaScript interface for accessing and manipulating HTTP pipeline portion, such requests and responses. It also provides a global fetch () method, which provides a simple and reasonable way to access resources across the network asynchronously

  • Specification and jQuery.ajax fetch () There are two different ways:
  1. When receiving a HTTP status code indicating an error, () returned from fetch Promise not flagged as reject, even if the status code of the HTTP response is 404 or 500. Instead, it is marked as a state Promise resolve (but ok resolve property will be set to the return value to false), only when a network failure or the request is blocked, it will be labeled as reject.
  2. By default, fetch does not send or receive any cookies from the server, if the site depends on the user session, the request will result in non-certified (to send cookies, you must set credentials option). Since August 2017 after 25, the default credentials for the policy change in same-originFirefox also change the default value 61.0b13
Carried fetch request

//fetch( url, config ).then().then().catch() methods: { get () { fetch(`${ BASE_URL }/get.php?a=1&b=2`)//get请求参数是连接在url上 .then( data => data.text() ) .then( res => { this.num = res }) .catch( err => console.log( err )) }, post () { /* 1. post请求参数如何携带 */ fetch(`${ BASE_URL }/post.php`,{ method: 'POST', headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' // 指定提交方式为表单提交 }), body: new URLSearchParams([["a", 1],["b", 2]]).toString() }).then( data => data.text() ) .then( res => { this.sum = res }) .catch( err => console.log( err )) }, getMovies () { /* 第一个then是为数据格式化,可以格式化的数据类型有: json text blob[ 二进制 ] 第二个then才是得到的数据 */ fetch('./mock/movie.json') .then( data => data.json() ) .then( res => { console.log("getMovies -> res", res) this.movies = res.movieList }) } }

  • Gets a JSON files over a network and print it to the console. The simplest is to use only one argument is used to indicate like fetch () resource path to, and then return Promise (a Response object) contains a response result.

Guess you like

Origin www.cnblogs.com/zengfanjie/p/11722855.html