作用相当于jquery中的ajax
用于网络请求
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
可以通过向axios传递相关配置来创建请求
axios(config)
axios(url?querystring [, config])
axios().then().catch()
axios().then(function(response){}).catch(function(err){})
// 发送 POST 请求
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
// 发送 POST 请求
axios('/user/12345', {
method: 'post',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
// 发送 GET 请求(默认的方法)
axios('/user/12345');
请求方法的别名
axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
例子:
get请求
axios.get(url?queryString).then(function(response){}).catch(function(err){})
// 为给定 ID 的 user 创建请求
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
// 可选地,上面的请求可以这样做
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
post请求
axios.post(url, {key1:value1,key2.value2}).then(function(response){}).catch(function(err){})
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});