Appearance
Ajax-Fetch-Axios 三者有什么区别
三者都用于网络请求,但是不同维度
- Ajax ( Asynchronous Javascript and XML),一种技术统称
- Fetch,一个具体的 API
- 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
2
3
4
5
6
7
8
9
10
11
12
Fetch
- 浏览器原生 API,用于网络请求
- 和 XMLHttpRequest 一个级别
- Fetch 语法更加简洁、易用,支持 Promise
javascript
function ajax2(url) {
return fetch(url).then(res => res.json())
}
1
2
3
2
3
Axios
- 最常用的网络请求 lib(随着 Vue 火爆起来)
- 内部可用 XMLHttpRequest 和 Fetch 来实现
- Axios,第三方库 https://axios-http.com
答案
- Ajax,一种技术统称
- Fetch,一个原生 API
- Axios,一个第三方库