axios数据请求
一、axios安装
npm install axios
cnpm install axios
yarn install axios
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
二、请求数据方法
axios({
method: 'get',
url: 'api',
params: {
key: value
},
headers: {
key: value
}
responseType: 'json'
}).then(response => {
let res = response.data;
console.log(res);
}).catch(error => {
console.log(error);
});
axios.get("api", {
params: {
key: value
},
headers: {
key: value
}
}).then(response => {
let res = response.data;
console.log(res);
}).catch(error => {
console.log(error);
});
let obj = qs.stringify({
key: value
});
axios({
method: 'post',
url: 'api',
data: obj,
headers: {
key: value
}
responseType: 'json'
}).then(response => {
let res = response.data;
console.log(res);
}).catch(error => {
console.log(error);
});
let data = {
key: value
},
headers = {
USERID: "",
TOKEN: ""
};
axios.post("api", qs.stringify(data), {
headers
}
}).then(response => {
let res = response.data;
console.log(res);
}).catch(error => {
console.log(error);
});
- Qs的使用
- 引用cdn或者使用npm、cnpm或者yarn进行插件安装
- 使用cdn时,默认全局变量为Qs
- Qs基本方法使用
- qs.stringify() 方法:将目标数据转换为string字符串
- qs.parse() 方法:将对象字符串格式的数据转换为对象格式