fetch 不是xhr,Fetch API和XMLHttpRequest有什么区别?

I know that fetch is using promises and both of them allow you to do AJAX requests to a server. I have read that fetch has some extra features, which aren't available in XMLHttpRequest (and in the fetch polyfill, since it's based on XHR).

What extra capabilities does the fetch API have?

解决方案

There are a few things that you can do with fetch and not with XHR:

You can use the Cache API with the request and response objects;

You can perform no-cors requests, getting a response from a server that doesn't implement CORS. You can't access the response body directly from JavaScript, but you can use it with other APIs (e.g. the Cache API);

Streaming responses (with XHR the entire response is buffered in memory, with fetch you will be able to access the low-level stream). This isn't available yet in all browsers, but will be soon.

There are a couple of things that you can do with XHR that you can't do yet with fetch, but they're going to be available sooner or later (read the "Future improvements" paragraph here: https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/):

Abort a request (this now works in Firefox and Edge, as @sideshowbarker explains in his comment);

Report progress.

This article https://jakearchibald.com/2015/thats-so-fetch/ contains a more detailed description.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值