Yifei Kong

May 30, 2017

用于替代ajax的 fetch API

fetch is the new and easy api for js to make request to the server, it replaces XMLHttpRequest.

Objects:

fetch, Request, Reponse, Headers

Usage:

fetch('/path/to/fetch',
    {method: 'GET'}
).then(function (response) {
    // process response;
    return response.json(); // returns a json promise
    return response.text(); // returns a text promise


    return ret; // will be consumed in the next then funcion
}).then(function (data) {
    // data is a json or text
    // process
}).catch(function (err) {

    // error handling
});

notice, the useful method

Advanced: