axios
Axios是专注于网络数据请求的库
相比于原生的XMLHttpRequest 对象,axios简单易用
相比于jQuery ,axios更加轻量化,只专注于网络请求
axios发起GET请求
// 为给定 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);
});
axios发起POST请求
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
直接使用axios发起请求
// 发送 POST 请求
axios({
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});
例子:
document.querySelector("#btn1").addEventListener('click',function(){
var url = 'http://www.liulongbin.top:3006/api/get';
var paramsObj = {
name: "zs" , age: 12