axios请求发送
0 抽取baseURL
- Vue项目可在项目根目录的
main.js
文件中抽取baseURL
axios.defaults.baseURL = 'http://localhost:8080/eduonline'
1 get请求
- 多个参数传递,一定是
params
表示
1.1 代码示例1
axios({
method: 'get',
url: 'page/getPageInfo',
headers: {
'content-type': 'application/json',
},
params: {
pos: this.position,
},
}).then(res => {
console.log('成功了')
console.log(res)
)
}, err => {
console.log('错误了')
console.log(err)
})
1.2 代码示例2
//可配置参数的方式
axios.get('/user',{
params:{
ID:12345
}
}).then(res => {
console.log('成功了')
console.log(res)
)
}, err => {
console.log('错误了')
console.log(err)
})
//或者直接在url后拼接参数
axios.get('/user?id=123')
.then(res => {
console.log('成功了')
console.log(res)
)
}, err => {
console.log('错误了')
console.log(err)
})
2 post请求
post
传递参数,必须用data
2.1 代码示例
//以json格式传入参数
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(res => {
console.log('成功了')
console.log(res)
)
}, err => {
console.log('错误了')
console.log(err)
})
//发送post请求
axios({
method: 'post', //请求方式,默认是get请求
url:'/user/12345', //地址
data:{ //参数
firstName: 'simon',
lastName: 'li'
}
});
2.1 后端接收参数为null,可以用FormData传递
- 项目中发现前端
post
请求传参,后端接收不到,均为null
值,此时可以通过FormData
进行传参
let formData = new FormData()
formData.append('position', this.position)
formData.append('docServerFileName', this.docServerFileName)
formData.append('socServerFileName', this.socServerFileName)
formData.append('vidServerFileName', this.vidServerFileName)
formData.append('posResponsibility', this.posResponsibility)
axios({
method: 'post',
url: 'page/submitPageInfo',
headers: {
'content-type': 'application/json',
},
data: formData,
}).then(res => {
console.log('成功了')
this.$Message.success('提交成功')
}, err => {
console.log('错误了')
console.log(err)
this.$Message.error('提交失败,请联系技术人员')
})
2.3 上传文件需修改content-type
- 上传文件,
headers
里的content-type
需改为multipart/form-data
let formData = new FormData()
formData.append('file', this.file)
axios({
method: 'post',
url: 'positionLearning/videoUpload/uploadVideo',
headers: {
'content-type': 'multipart/form-data',
},
data: formData,
}).then(res => {
console.log('成功了')
console.log(res)
}, err => {
console.log('错误了')
console.log(err)
this.$Message.error('后台服务出问题,请联系技术人员')
})
6 comments
2025年10月新盘 做第一批吃螃蟹的人coinsrore.com
新车新盘 嘎嘎稳 嘎嘎靠谱coinsrore.com
新车首发,新的一年,只带想赚米的人coinsrore.com
新盘 上车集合 留下 我要发发 立马进裙coinsrore.com
做了几十年的项目 我总结了最好的一个盘(纯干货)coinsrore.com
新车上路,只带前10个人coinsrore.com
新盘首开 新盘首开 征召客户!!!coinsrore.com
新项目准备上线,寻找志同道合 的合作伙伴coinsrore.com
新车即将上线 真正的项目,期待你的参与coinsrore.com
新盘新项目,不再等待,现在就是最佳上车机会!coinsrore.com
新盘新盘 这个月刚上新盘 新车第一个吃螃蟹!coinsrore.com
新盘首开 新盘首开 征召客户!!!
字里行间流露出真挚的情感,让人感同身受,共鸣不已。
哈哈哈,写的太好了https://www.lawjida.com/
《忠实的朋友》喜剧片高清在线免费观看:https://www.jgz518.com/xingkong/97308.html
《阿涅斯的海滩》记录片高清在线免费观看:https://www.jgz518.com/xingkong/16457.html