Request - Fetch

The concept and usage

  • It provides a general definition of the Request (request), and the Response (response) (and other network-related requests) object
  • It can be used to more application scenarios: either service workers, Cache API, or is otherwise deal with requests and responses, or even any kind of way you need to generate a response in the program.
  • There is a concept of relevance, e.g. native CORS and HTTP headers, to provide a new definition, substituents that separate their original definition.
  • On the Window and WorkerGlobalScope interface implements this method. So you can get resources to use this method in almost all environments.
  • fetch () must accept a parameter - path to the resource. You can also pass an optional second parameter init
  • Whether the request was successful or not, it returns a Promise objects
  • By Request () and Response () constructor to create a direct request and response, but we do not recommend it. They should be used to create other results of the API (for example, service workers in FetchEvent.respondWith).

Suspend fetch

  • Browser has begun AbortController and AbortSignal interfaces (ie Abort API) to add experimental support, which allows XHR like Fetch and such operations were discontinued in time has not been completed.

Fetch

  • fetch () method initiates a request for access to resources. It returns a promise, this promise will respond to the request after the resolve, and returns the Response object.
  • Window and WorkerGlobalScope have achieved WorkerOrGlobalScope. - This means that if you want to get basic resources, you can use WorkerOrGlobalScope located in the fetch () method in any scene.
  • When encountered a network error, fetch () returns the promise will be reject, and return TypeError (HTTP 404 status is not considered a network error.)
  • fetch () method is controlled by the Content Security Policy connect-src command, rather than its resource requests.
    • Content Security Policy HTTP response header allows site administrators to control user agent which resources can be loaded for the specified page. Will help prevent XSS (Cross-Site Script) (XSS)
    • connect-src restrict URL via scripting interface loaded.

grammar

Promise<Response> fetch(input[, init]);

parameter

  • input custom resource to get. this might be:
    • USVString a string containing the URL to retrieve the resource.
      • In addition to not allow proxy code unpaired outside, USVString equivalent to DOMString?
      • UTF-16 is a DOMString string
    • A Request object
  • init a configuration object, all of the settings to the request. The optional parameters are:
    • method: a method using a request, such as GET, POST.
    • headers: header information request, in the form of Headers object or objects contained ByteString literal values.
    • body: body information request: there may be a Blob, BufferSource, FormData, URLSearchParams USVString or objects. Note that a method of GET or HEAD request information does not contain body.
      • BufferSource a type, and comprises ArrayBuffer ArrayBufferView.
    • mode: mode request, such as cors, no-cors or same-origin.

Guess you like

Origin www.cnblogs.com/qq3279338858/p/11057795.html