import axios from ‘axios’
Vue.prototype.$axios = axios
在组件中使用axios
==========================================================================
-
get:获取数据,请求指定的信息,返回实体对象
-
post:向指定资源提交数据(例如表单提交或文件上传)
-
put:更新数据,从客户端向服务器传送的数据取代指定的文档的内容
-
patch:更新数据,是对put方法的补充,用来对已知资源进行局部更新
-
delete:请求服务器删除指定的数据
示例代码
方法一
//请求格式类似于 http://localhost:8080/goods.json?id=1
this.$axios.get(‘/goods.json’,{
params: {
id:1
}
}).then(res=>{
console.log(res.data);
},err=>{
console.log(err);
})
方法二
this.$axios({
method: ‘get’,
url: ‘/goods.json’,
params: {
id:1
}
}).then(res=>{
console.log(res.data);
},err=>{
console.log(err);
})
post请求一般分为两种类型
1.form-data
表单提交,图片上传、文件上传时用该类型比较多
2. application/json
一般是用于 ajax 异步请求
示例代码
方法一
this.$axios.post(‘/url’,{
id:1
}).then(res=>{
console.log(res.data);
},err=>{
console.log(err);
})
方法二
$axios({
method: ‘post’,
url: ‘/url’,
data: {
id:1
}