iViewadmin数据请求跨域处理

main.js中的要改成这样

import axios from 'axios';
var instance = axios.create({
baseURL: 'http://192.168.10.67/api',
timeout: 5000,
});
Vue.prototype.$axios = instance;

在logo.vue页面

var  params = {
userName:__this.form.userName,
userPwd:__this.form.password
};
if (__this.form.userName !== '' && __this.form.password !== '') {
__this.$axios.post('/login/checkLogin',JSON.stringify(params), {
headers: {
"Content-Type":'application/json'
}})
.then(response=>{
if(response.status == "200"){
Cookies.set("token",response.data.data);
if(response.data.msg =="success"){
let admin_info = eval("("+response.config.data+")");
console.log(admin_info);
let ___this = __this;
___this.show = false;
if (valid) {
Cookies.set('user', ___this.form.userName);
Cookies.set('password', ___this.form.password);
___this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
if (___this.form.userName === admin_info.userName && ___this.form.password === admin_info.userPwd) {
Cookies.set('access', 0);
} else {

Cookies.set('access', 1);
}
___this.$router.push({
name: 'home_index'
});
}
}else{
console.log("登录失败");
}
}else {
throw new Error("sss");
}
}).then(data=>{
})

转载于:https://www.cnblogs.com/boonook/p/8949361.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中发送POST请求跨域处理可以通过两种方式来实现:使用代理或者设置服务器端的CORS。 1. 使用代理: 在Vue项目的根目录下找到`vue.config.js`文件(如果没有则新建一个),在该文件中添加以下代码: ```javascript module.exports = { devServer: { proxy: { '/api': { target: 'http://api.example.com', // 后端接口的域名 changeOrigin: true, pathRewrite: { '^/api': '' // 如果后端接口没有/api前缀,可以将此行注释掉 } } } } } ``` 上述代码中,`target`是指向后端接口的域名,`changeOrigin`设置为`true`表示开启跨域,`pathRewrite`用于将请求路径中的`/api`替换为空字符串(如果后端接口没有/api前缀,可以将此行注释掉)。 然后,在前端代码中发送POST请求时,将请求路径改为`/api/xxx`即可,例如: ```javascript axios.post('/api/login', { username: 'admin', password: '123456' }) .then(response => { // 处理响应数据 }) .catch(error => { // 处理错误 }); ``` 2. 设置服务器端的CORS: 如果你无法修改Vue项目的配置文件,或者你希望通过服务器端来处理跨域问题,可以在服务器端设置CORS。以Node.js的Express框架为例,在后端接口的路由处理函数中添加以下代码: ```javascript app.use(function(req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept'); next(); }); ``` 上述代码中,`Access-Control-Allow-Origin`设置为`*`表示允许任意域名的请求,你也可以将其设置为指定的域名。`Access-Control-Allow-Methods`用于设置允许的HTTP方法,`Access-Control-Allow-Headers`用于设置允许的请求头。 请根据你的具体情况选择其中一种方式来处理跨域问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值