千峰商城-springboot项目搭建-41-axios异步请求方法
axios提供了多种异步请求方法,实现对RESTful风格的支持。
1.get请求
axios.get(url).then(function);
axios.get(url,{}).thrn(function);
//使用axios的get请求传递参数,需要将参数设置在params下
axios.get("http://localhost:9999/music/detail",{
params:{
id:"28059417"
}
})
.then(function(res){
console.log(res);
});
2.post请求
axios.post(url,{}).thrn(function);
axios.post("http://localhost:9999/music/search",{s:"张杰"})
.then(function(res){
console.log(res);
});
3.自定义请求:自定义请方式、请求参数、请求头、请求体(仅限于post)
axios({
url:"http://localhost:9999/music/search",
method:"post",
params:{
//设置请求行传值
s:"成都",
limit:15
},
headers:{
//设置请求头
},
data:{
//设置请求体(仅限于post/put)
}
}).then(function(res){
//回调
console.log(res);
});