我知道 Fetch API 使用 Promise 并且它们都允许您向服务器发出 AJAX 请求.
I know that Fetch API uses Promises and both of them allow you to do AJAX requests to a server.
我了解到 Fetch API 有一些额外的功能,这些功能在 XMLHttpRequest 中不可用(在 Fetch API polyfill 中,因为它基于 XHR).
I have read that Fetch API has some extra features, which aren't available in XMLHttpRequest (and in the Fetch API polyfill, since it's based on XHR).
Fetch API 有哪些额外功能?
What extra capabilities does the Fetch API have?
有些事情你可以用 fetch 而不是 XHR:
There are a few things that you can do with fetch and not with XHR:
no-cors 请求,从未实现 CORS 的服务器获取响应.您无法直接从 JavaScript 访问响应正文,但您可以将其与其他 API(例如缓存 API)一起使用;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);你可以用 XHR 做一些你不能用 fetch 做的事情,但它们迟早会可用(阅读此处的未来改进"段落:https://hacks.mozilla.org/2015/03/this-api-就是这么拿取/):
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/):
本文https://jakearchibald.com/2015/thats-so-fetch/ 包含更详细的描述.
This article https://jakearchibald.com/2015/thats-so-fetch/ contains a more detailed description.
这篇关于获取 API 与 XMLHttpRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
即使在调用 abort (jQuery) 之后,浏览器也会等待Browser waits for ajax call to complete even after abort has been called (jQuery)(即使在调用 abort (jQuery) 之后,浏览器也会等待 ajax 调用
JavaScript innerHTML 不适用于 IE?JavaScript innerHTML is not working for IE?(JavaScript innerHTML 不适用于 IE?)
XMLHttpRequest 无法加载,请求的资源上不存在“AXMLHttpRequest cannot load, No #39;Access-Control-Allow-Origin#39; header is present on the requested resource(XMLHttpRequest 无法加载,请求的资
XHR HEAD 请求是否有可能不遵循重定向 (301 302)Is it possible for XHR HEAD requests to not follow redirects (301 302)(XHR HEAD 请求是否有可能不遵循重定向 (301 302))
NETWORK_ERROR:XMLHttpRequest 异常 101NETWORK_ERROR: XMLHttpRequest Exception 101(NETWORK_ERROR:XMLHttpRequest 异常 101)
XMLHttpRequest 206 部分内容XMLHttpRequest 206 Partial Content(XMLHttpRequest 206 部分内容)