vue+axios跨域访问

使用axios跨域访问的时候浏览器报错:


我的代码:

1.main.js配置:

import axios from 'axios';

axios.defaults.timeout = 5000;// 在超时前,所有请求都会等待 5 秒
axios.defaults.headers.post['Content-Type']= application/x-www-form-urlencoded;charset=UTF-8';// 配置请求头
axios.defaults.baseURL = 'http://*******';// 配置接口地址
axios.defaults.withCredentials = false;

Vue.prototype.$axios = axios;// 将axios配置成vue的原型
2.在文件中使用:
this.$axios.post('/', {
        method: 'user.login',
        mobile: this.username,
        password: this.password,
        sign: ''
      }).then(function (response) {
        console.log(response);
      }).catch(function (error) {
        console.log(error);
      });

我们都以为是后端写的有问题,找了很久,最后发现只需要将axios传递的json格式的数据转换成string格式就可以了

this.$axios.post('/', JSON.stringify({//这里将json格式转成string格式发送
method: 'user.login',
mobile: this.username,
password: this.password,
sign: ''
})).then(function (response) {
console.log(response);
}).catch(function (error) {
console.log(error);
});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mosowe

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值