一、基本介紹
axios是一個基于Promise的(de)HTTP客戶端(duan),可用于瀏(liu)覽器(qi)和node.js。
axios的特點:
從瀏覽器中創建XMLHttpRequests 從node.js創建http請求 支持Promise API 攔截請求和響應 轉換請求數據和響應數據 取消請求 自動轉換JSON數據 客戶端支持防止CSRF本文將(jiang)詳細介紹axios.put方(fang)法,說明(ming)其使(shi)用(yong)方(fang)法、參數、特點和應用(yong)場景。
二、基本用法
在使用axios.put方法時,需要傳入url和(he)data兩個參數(shu)。
其中,url為請(qing)求的(de)接口地(di)址,data為請(qing)求的(de)參數。
axios.put('/api/user', {
name: 'john',
age: 18
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
三、參數詳解
axios.put方法可以接收三個參數,分別(bie)是url、data和config。
1. url
url為(wei)請求的接口地(di)址(zhi),可以(yi)是相對(dui)路徑或絕(jue)對(dui)路徑。
axios.put('/api/user', {
name: 'john',
age: 18
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
2. data
data為(wei)請求的參數。
axios.put('/api/user', {
name: 'john',
age: 18
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3. config
config為請求(qiu)的配置(zhi)選(xuan)項,包括headers、params、timeout等。
axios.put('/api/user', {
name: 'john',
age: 18
}, {
headers: {'X-Requested-With': 'XMLHttpRequest'},
params: {id: 1},
timeout: 1000
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
四、特點
axios.put方(fang)法的特點(dian)如下:
1. 支持Promise API
axios.put方法返回(hui)的是一個Promise對象,可用于鏈式調用。
axios.put('/api/user', {
name: 'john',
age: 18
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
2. 支持攔截器
axios.put方法(fa)支持請求攔截器(qi)和響(xiang)應攔截器(qi),用于對請求和響(xiang)應進行全局(ju)處理。
// 添加請求攔截器
axios.interceptors.request.use(function (config) {
// 在請求發送之前做一些處理
return config;
}, function (error) {
// 對請求錯誤做些什么
return Promise.reject(error);
});
// 添加響應攔截器
axios.interceptors.response.use(function (response) {
// 對響應數據做些什么
return response;
}, function (error) {
// 對響應錯誤做些什么
return Promise.reject(error);
});
// 發送put請求
axios.put('/api/user', {
name: 'john',
age: 18
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3. 支持取消請求
axios.put方(fang)法支持(chi)取(qu)消請求(qiu),用于(yu)在請求(qiu)發送之前或(huo)請求(qiu)返(fan)回之后取(qu)消請求(qiu)。
// 創建取消請求的對象
var cancelToken = axios.CancelToken;
var source = cancelToken.source();
// 發送put請求
axios.put('/api/user', {
name: 'john',
age: 18
}, {
cancelToken: source.token
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// 取消請求
source.cancel('取消請求');
五、應用場景
axios.put方法(fa)可以用于(yu)以下一些場景:
1. 更新數據
axios.put可以用(yong)于更新數(shu)據(ju)。
axios.put('/api/user', {
id: 1,
name: 'john',
age: 18
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
2. 整體替換數據
axios.put可(ke)以用(yong)于整體替換數據。
axios.put('/api/user', {
id: 1,
address: 'beijing'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
3. 批量更新數據
axios.put可以用于批量更新數據。
axios.put('/api/user/batch', [
{id: 1, name: 'john'},
{id: 2, name: 'mike'}
])
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});