用于替代ajax的 fetch API


Author: yifei / Created: May 30, 2017, 11:45 a.m. / Modified: May 30, 2017, 11:52 a.m. / Edit

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:


有任何问题可以发邮件到 kongyifei (at) gmail.com 讨论