1.vue cli下载axios
npm install axios
2.注册使用
在main.js中引入import axios from 'axios'
然后在Vue原型上添加Vue.prototype.$axios = axios
3.get请求
this.$axios或者axios
1.参数一表示请求地址
2.参数二表示配置信息
3.params表示传递到服务器端的数据,以url参数的形式拼接在请求地址后面
{ page:2,per:3 }
比如:https://api.cat-shop.penkuoer.com/api/v1/products
最终生成的url为
https://api.cat-shop.penkuoer.com/api/v1/products?page=2&per=3
4.headers表示请求头
axios.get("https://api.cat-shop.penkuoer.com/api/v1/products",{
params:{
page:2,
per:3
},
headers:{}
}).then( res =>{
console.log(res)
})
4.post请求
1.参数一请求地址
2.参数二传递的数据,在请求体中传递
{ 注意事项:
1.axios默认发送的数据是json格式的
2.默认的配置项里面有一个请求头headers,请求头里面有个默认属性conttent-type:'application/json'所以传递给服务器数据默认就是json格式的
}
3.参数三配置信息