Difference between fetch, ajax, and xhr(fetch、ajax 和 xhr 之间的区别)
问题描述
What is the difference between these 3 calling methods? I'm using fetch in my current project and don't see any real difference between them. Why does there need to be 30 different ways to do things in javascript XD.
Thanks.
Ajax is a buzzword meaning "Making an HTTP request from JavaScript without leaving the page".
XMLHttpRequest
and fetch
are APIs, provided by browsers, which allow HTTP requests to be made from JavaScript.
XMLHttpRequest
has been around since the 90s and is event-driven, requiring that you bind event listeners to detect when data has arrived.
fetch
is newer and built around Promises, which are now the prefered way to do asynchronous operations in JavaScript. It is sufficiently well established to be supported everywhere significant except in Internet Explorer. It does, however, lack certain features (such as progress monitoring) that `XMLHttpRequest provides.
So in short, there are two (not 30) purpose-built ways to do Ajax, and one of them is modern.
For practical introductions on how to use either of them, MDN has you covered:
- Using XMLHttpRequest
- Using Fetch
There are also various libraries which wrap around fetch
or XMLHttpRequest
to provide APIs which are situationally more convenient (e.g. a consistent API between Node.js and browsers or integration with a framework like Angular).
这篇关于fetch、ajax 和 xhr 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:fetch、ajax 和 xhr 之间的区别
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Fetch API 如何获取响应体? 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01