Skip to content
On this page

Ajax-Fetch-Axios 三者有什么区别

三者都用于网络请求,但是不同维度

  1. Ajax ( Asynchronous Javascript and XML),一种技术统称
  2. Fetch,一个具体的 API
  3. Axios,第三方库 https://axios-http.com

ajax

javascript
function ajax1(url, successFn) {
    const xhr = new XMLHttpRequest()
    xhr.open("GET"url, false)
    xhr.onreadystatechange = function () {
    // 这里的函数异步执行,可参考之前 ]S 基础中的异步模块
    if (xhr.readyState == 4) {
        if (xhr.status == 200 ){
            successFn(xhr.responseText)
        }
    }
    xhr.send(null)
}
1
2
3
4
5
6
7
8
9
10
11
12

Fetch

  1. 浏览器原生 API,用于网络请求
  2. 和 XMLHttpRequest 一个级别
  3. Fetch 语法更加简洁、易用,支持 Promise
javascript
function ajax2(url) {
    return fetch(url).then(res => res.json())
}
1
2
3

Axios

  1. 最常用的网络请求 lib(随着 Vue 火爆起来)
  2. 内部可用 XMLHttpRequest 和 Fetch 来实现
  3. Axios,第三方库 https://axios-http.com

答案

  1. Ajax,一种技术统称
  2. Fetch,一个原生 API
  3. Axios,一个第三方库
沪ICP备20006251号-1