参考文章:fetch API和传统ajax接口的差别
机制:
- fetchAPI支持stream读取
- fetchAPI可以控制Header、Response(需要把Response.json().then()来调用)、Request
- FetchAPI更支持异步Promise调用,而不是使用异步的CB调用
- 相对起XHR,你可以用更简便的使用方式就可以发起异步请求。比起$,fetch的优势在于这是原生的。
使用方法差别:
XHR:
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.onload = function() {
console.log(xhr.response);
};
xhr.onerror =