axios获取发起请求的地址 vue_axios安装,发送地址请求实例

本文介绍了如何在Vue项目中使用axios进行GET和POST请求,包括携带参数的方法、并发请求的处理,以及如何创建axios实例和设置全局配置。还讲解了axios的拦截器用法,用于在请求前后的数据处理和错误处理。
摘要由CSDN通过智能技术生成

安装

先在当前项目安装axios

cnpm i axios -S

get请求

axios.get('http://localhost:8082/getAllStudents').then(res=>{

console.log(res);

}).catch(err=>{

console.log(err);

});

上面这个例子并没有带参数过去,如果需要携带参数可以有2种写法。

方法1

直接在请求的地址里,写上?key=value

axios.get('http://localhost:8082/getStudentsByName?name=marry').then(res=>{

console.log(res);

}).catch(err=>{

console.log(err);

});

方法2

axios.get('http://localhost:8082/getStudentsByName', {

params : {

name : 'tom'

}

}).then(res=>{

console.log(res);

}).catch(err=>{

console.log(err);

});

post请求

post请求与get请求类似,不同的是,如果post请求需要携带参数过去给服务器,需要把参数转换成key=value&key=value这种形式。

axios.post('http://localhost:8082/getStudentsByName', qs.stringify({

name : 'tom'

})).then(res=>{

console.log(res);

}).catch(err=>{

console.log(err);

});

这里的qs.stringify是笔者写的一个方法而已,代码如下 :

var qs = {

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值