axios 跨域传参_vue.js学习之 跨域请求代理与axios传参

vue.js学习之 跨域请求代理与axios传参

一:跨域请求代理

1:打开config/index.js

module.exports{

dev: {

}

}

在这里面找到proxyTable{},改为这样:

proxyTable: {

'/api': {

target: 'http://121.41.130.58:9090',//设置你调用的接口域名和端口号 别忘了加http

changeOrigin: true,

pathRewrite: {

'^/api': ''//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可

}

}

}

2:在需要调接口的组件中这样使用:

axios.post('/api/yt_api/login/doLogin',postData)

.then(function (response) {

console.log(1)

console.log(response);

})

.catch(function (error) {

console.log(error);

})

注意:原接口:http://http://121.41.130.58:9090/yt_api/login/doLogin

页面调用:http://localhost:8081/api/yt_api/login/doLogin ——这里多了一个/api/不是多余的,不要删

二:axios传参

1:Vue官方推荐组件axios默认就是提交 JSON 字符串,所以我们要以json字符串直接拼接在url后面的形式,直接将json对象传进去就行了

let postData = {

companyCode:'tur',

userName:'123456789123456789',

password:'123456'

}

axios.get('/api/yt_api/login/doLogin',{

params: {

...postData,

}

})

.then(function (response) {

console.log(1)

console.log(response);

})

.catch(function (error) {

console.log(error);

});

这里我们将postData这个json对象传入到post方法中,页面中的形式为:

200e7587e3b20bb7c16f170d28f2bc0d.png

2:以key:val的形式传参

let postData = qs.stringify({

companyCode:'tur',

userName:'123456789123456789',

password:'123456'

})

我们需要对这个json对象操作,这里的qs你需要先安装

npm install qs

再导入

import qs from 'qs'

面中的形式为:

172f88295d3b7cbdd2fcf2bca2a0d093.png

最新链接:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值