Skip to content

Latest commit

 

History

History
38 lines (30 loc) · 914 Bytes

网络请求.md

File metadata and controls

38 lines (30 loc) · 914 Bytes

ajax/axios/fetch的区别

ajax前后端数据通信

const xhr = new XMLHttpRequest();
xhr.open('get','http://127.0.0.1:3000/user/list')
xhr.onreadystatechange = function(){
    if(xhr.readyState === 4 && xhr.status === 200){
        let text = xhr.responseText
    }
}
xhr.send()

axios

axios也是对ajax的封装,核心也是XMLHttpRequest,但是基于Promise管理请求,可以解决回调地狱问题,一般需要对axios进行二次封装

fetch

fetch是 ES6 新增的数据通信的新方案,本身基于promise管理

async function(){
    const res = await fetch('http://127.0.0.1:3000/user/login', {
        method:'post',
        header:{
            'Content-Type':'application/x-www-form-urlencoded'
        },
        body:Qs.stringify({
            userName:'fltenwall',
            password:md5('1111111')
        })
    })
}