vue.js okhttp 前端 3分钟让你学会axios在vue项目中的基本用法(建议收藏) - console
import axios from ‘axios’
Vue.prototype.$axios = axios
在组件中使用axios
三、Axios请求方式
========================================================================== 拼多多帮助力入口
1、axios可以请求的方法:
get:获取数据,请求指定的信息,返回实体对象
post:向指定资源提交数据(例如表单提交或文件上传)
put:更新数据,从客户端向服务器传送的数据取代指定的文档的内容
patch:更新数据,是对put方法的补充,用来对已知资源进行局部更新
delete:请求服务器删除指定的数据
2、get请求
示例代码
方法一
//请求格式类似于 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);
})
3、post请求
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
}
文章来源