首先安装axios
插件,使用该插件发送请求。
get请求
发送get请求
axios
.get('http://127.0.0.1:8080/api')
.then(response => (this.msg = response))
.catch(error => console.log(error))
get传参
axios
.get('http://127.0.0.1:8080/api',{
params:{
"name":"zs",
"age":18
}
})
.then(response => (this.msg = response))
.catch(error => console.log(error))
post请求
因为vue发送到后端之后是json数据,需要处理一下,在定义vue实例的文件中添加如下配置:
import qs from 'qs';
Vue.prototype.$qs = qs;
发送post请求
axios
.post('http://127.0.0.1:8080/api',this.$qs.stringify({
'arg1':'hello',
'arg2':'world!'
}))
.then(response => (this.msg = response))
.catch(error => console.log(error))