fetch、ajax 和 xhr 的区别 [英] Difference between fetch, ajax, and xhr

查看:33
本文介绍了fetch、ajax 和 xhr 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这3种调用方式有什么区别?我在我当前的项目中使用 fetch,并没有看到它们之间有任何真正的区别.为什么在 javascript 中需要有 30 种不同的方式来做事 XD.

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.

谢谢.

推荐答案

Ajax 是一个流行语,意思是在不离开页面的情况下从 JavaScript 发出 HTTP 请求".

Ajax is a buzzword meaning "Making an HTTP request from JavaScript without leaving the page".

XMLHttpRequestfetch 是浏览器提供的 API,允许从 JavaScript 发出 HTTP 请求.

XMLHttpRequest and fetch are APIs, provided by browsers, which allow HTTP requests to be made from JavaScript.

XMLHttpRequest 自 90 年代以来就已经存在,并且是事件驱动的,需要您绑定事件侦听器以检测数据何时到达.

XMLHttpRequest has been around since the 90s and is event-driven, requiring that you bind event listeners to detect when data has arrived.

fetch 是更新的并围绕 Promises 构建,它现在是在 JavaScript 中执行异步操作的首选方式.除了 Internet Explorer 之外,它在任何重要的地方都被支持.但是,它确实缺少`XMLHttpRequest 提供的某些功能(例如进度监控).

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.

简而言之,有两种(不是 30 种)专门构建的 Ajax 方法,其中一种是现代的.

So in short, there are two (not 30) purpose-built ways to do Ajax, and one of them is modern.

有关如何使用其中任何一个的实用介绍,MDN 已涵盖:

For practical introductions on how to use either of them, MDN has you covered:

还有各种各样的库围绕着 fetchXMLHttpRequest 来提供更方便的 API(例如,Node.js 和浏览器之间的一致 API 或与像 Angular 这样的框架).

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 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆