axios
安裝
npm install axios
請(qǐng)求
improt Axios from 'axios'
Axios.get(url, config)
Axios.post(url, data, config)
Axios.post('/webapi/book/info', {
bookId: 7075545,
user_id: 8000000,
timestamp: 1576812779109,
sign: 'fc4a2b719a4049cdb09dc38da3686b1e',
shuqi_h5: ''
}, {
transformRequest (data) {
// 會(huì)自動(dòng)接收到 data 參數(shù)粘招。需要 返回辨图,返回的內(nèi)容就是請(qǐng)求參數(shù)的內(nèi)容
// { nodeId: 70022794, pagesize: 3, pageidx: 1 }
//=>nodeId=70022794&pagesize=3&pageidx=1
// 提供一個(gè)數(shù)組
let arr = []
// 遍歷對(duì)象
for (let key in data) {
arr.push(`${key}=${data[key]}`)
}
// 返回并 join 相當(dāng)于轉(zhuǎn)換成了from格式的請(qǐng)求
return arr.join('&')
}
params: {
_: new Date().getTime()
}
}).then(response => {
const { data } = response
console.log(data)
})
fetch
安裝
npm install fetch
請(qǐng)求
improt fecth from 'fetch'
fetch("**",{
credentials:"include",
method:'post',
headers: {
"Content‐Type": "application/x‐www‐form‐urlencoded"
},
body: "name=kerwin&age=100",
//上為form傳參,后為jsom傳參
headers: {
"Content‐Type": "application/json"
},
body: JSON.stringify({
name:"kerin",
age:100
})
}).then(res=>res.json()).then(res=>{console.log(res)});