js跨域名传参_vue跨域问题及post传参

方案一:

在config.index.js中设置webpack

最初的方案是前台传JSON格式的数据过去;设置’Content-Type’:

'application/json;charset=UTF-8',大概:

axios.post('/user',JSON.stringify(params),{

headers: {

'Content-Type': 'application/json;charset=UTF-8'

}

})

.then(function (res) {

console.log(res);

})

.catch(function (error) {

console.log(error);

});

并且后台改变其接收参数的方式改为@RequestBody。

此方法主要参考该博客:https://blog.csdn.net/CarryBest/article/details/80079364

该博客有个问题,就是接收单个参数的时候,是可行的,但是要是接收多个参数,仍然会报错。解决方法是只要后台设置接收的参数为Map类型或是多个参数组成的类的这种类型

方案二

使用URLSearchParams的API:

const params = new URLSearchParams();

params.append('param1', 'value1');

params.append('param2', 'value2');

axios.post('/foo', params);

但是该API的浏览器兼容性不是很理想,不过有polyfill,提供了兼容性方案。(https://github.com/WebReflection/url-search-params )

代码

module.exports = {

dev: {

// Paths

assetsSubDirectory: 'static',

assetsPublicPath: '/',

proxyTable: {

'/api': {

target: ' http://172.17.186.222:8999/',

changeOrigin: true,

pathRewrite: {

'^/api': ''

}

}

},

methods: {

login(forName) {

this.$refs[forName].validate(valid => {

if (valid) {

this.$axios.post('api/user/login',{

name:this.loginForm.username,

password:this.loginForm.password,

},

{ headers: { 'Content-Type': 'application/json' }

}

).then(res=>{

if(res.data.code===0){

Cookies.set("userName",res.data.data.username);

this.userName=res.data.data.username;

this.$router.push("/index/statistics");

}else if(res.data.code ===50010){

this.$message.closeAll();

this.$message({

duration: 1600,

message: "用户名或密码错误!",

type: "error"

});

}

else{

this.$message.closeAll();

this.$message({

message: res.data.msg,

type: "error"

});

}

})

} else {

console.log("error submit!!");

return false;

}

});

}

}

参考

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值