vue中axios引用
import axios from 'axios'
axios.defaults.baseURL="http://localhost:8888";
Vue.prototype.$axios = axios
axios 传值
get方式 :
1. url后面加参数
this.$axios.get("http://localhost:8888/select.do?username='hahha'");
2. params
this.$axios.get("http://localhost:8888/select.do",{
params:{
username:"hahah"
}
});
post方式:
1. 字符串形式
this.$axios.post("http://localhost:8888/postselect.do","username='hahha'");
2. transformRequest方式(需要自己写transformRequest方法,不推荐)
this.$axios.post("http://localhost:8888/postselect.do",{username:'hahha'} ,{
transformRequest:[
function(data){
// username=hahha&age=18
let params = "";
var arr = [];
for(var key in data){
arr.push(key+"="+data[key]);
}
params = arr.join("&");
return params;
}
]
});
3. 使用qs模块(推荐)
import qs from 'qs'
this.$axios.post("http://localhost:8888/postselect.do",qs.stringify({username:'hahha'}));
4. 使用JSON.stringify方法(强烈推荐,不需要安装)
this.$axios.post("http://localhost:8888/postselect.do",JSON.stringify({username:'hahha'}));
作者:doubleyong(个人网站:www.bugshouji.com 微信公众号:bug收集)