Home > Web Front-end > Front-end Q&A > What is the usage of fetch in es6

What is the usage of fetch in es6

WBOY
Release: 2022-05-06 18:10:02
Original
3079 people have browsed it

In es6, fetch is used to initiate a request for remote resources and provides a method to obtain resources asynchronously across the network; this method is defined in the window object of the BOM and returns a Promise object, with the syntax "fetch(url, Configuration object).then(function(response){})".

What is the usage of fetch in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What is the usage of fetch in es6

Basic syntax of fetch

fetch(url,init).then(function(response) { } )
Copy after login

es6 How to use fetch?JavaScript

Fetch parameter description

  • fetch receives two parameters, the first is the address and is required, and the second is the configuration object and is optional.

  • #If it is a simple get request without parameters, then the second parameter is not required (the default is get request), of course, you can also add some explanation on this fetch

  • The second parameter contains the request type, sent data, headers, mode, etc.

  • The fetch method also returns a For promise objects, we can only use then to obtain return data.

  • We need two thens to process the data returned from the background, and return result in the first then .text(), or return result.json(), and then the specific returned value can be truly obtained in the second parameter and processed logically

  • If you want to judge If the request fails, please judge it in the first then, which contains the request data object.

The Fetch API provides a JavaScript interface for accessing and manipulating parts of the HTTP pipeline, such as requests and responses. It also provides a global fetch() method that provides a simple, reasonable way to obtain resources asynchronously across the network.

This function was previously implemented using XMLHttpRequest. Fetch provides a better alternative that can be easily used by other technologies, such as Service Workers. Fetch also provides a single logical location to define other HTTP-related concepts, such as CORS and extensions to HTTP.

The Fetch API provides a fetch() method, which is defined in the window object of the BOM. You can use it to initiate a request for remote resources. This method returns a Promise object, allowing you to retrieve the return results of the request.

fetch only supports cross-domain CORS and does not support JSONP spanning

<script>
    //fetch发送数据
    //支持CORS跨域,没有办法接受jsonp数据
    function getData() {
        //支持 cors跨域url地址&#39;http://api.yytianqi.com/air?city=CH010100&key=2c5br4sgmguremgg&#39;
        //https://api.douban.com/v2/book/1220562?callback=func
       return fetch(&#39;http://localhost:3001/getdata&#39;)
            .then(function (response) {
                console.log(response);
                //promise对象返回
                return response.json();
            })
    }
    getData().then(function (data) {
        console.log(data);
    })
</script>
Copy after login

[Related recommendations: javascript video tutorial, web front-end

The above is the detailed content of What is the usage of fetch in es6. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
es6
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template