url全局地址封装 vue_vue设置全局访问接口API地址操作

该博客介绍了如何在Vue项目中进行全局API接口的封装,包括本地和线上接口地址的定义,以及axios的使用方法,例如登录功能的实现,涉及登录错误处理和登录成功的页面跳转。
摘要由CSDN通过智能技术生成

methods: {

ajaxLoginApi: function (form) {

var that=this.form; // 放置指针,便于then操作的获取

console.log("账号"+that.username+"密码"+that.pwd);

var localPath = this.GLOBAL.localSrc; // 本地 接口地址 【注释1】

var serverPath = this.GLOBAL.serverSrc; // 线上 接口地址【注释2】

axios.post( serverPath + '/login',   // 【注释3】

{

// 'userName':'17681135201',

// 'userPwd':'123456',

// 'userType':"2"

'userName': this.form.username,

'userPwd': this.form.pwd,

'userType':"2"

},

{

headers: {

'Content-Type':'application/json',

//'Authorization':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiI1QUZFRkQ4NTNFNjIwNDIyOEIwNDE3MzdCMDdBNEJCNURDNTBEQjA4OUFCQzBENDM4MDA5RkM4REU4QkMyODkzIiwidXNlck5hbWUiOiIxNzY4MTEzNTIwMSIsInVzZXJBZ2VudCI6Ik1vemlsbGEvNS4wIChXaW5kb3dzIE5UIDEwLjA7IFdpbjY0OyB4NjQpIEFwcGxlV2ViS2l0LzUzNy4zNiAoS0hUTUwsIGxpa2UgR2Vja28pIENocm9tZS82OC4wLjM0NDAuMTA2IFNhZmFyaS81MzcuMzYiLCJleHAiOjE1NTE5MzYzODQsIm5iZiI6MTU1MTkzNDU4NH0.YgRW-Wnlk_Gz64id_jTocH68oj8DuZuI1WyQBmmrt7c'

}

},

)

.then(function (response) {

console.log("response="+response);

console.log("responseheader="+response.headers);

var reslutData = response;

// console.log(reslutData.data.code);

// 登录账号和密码 检测

if (reslutData.data.code == 2006) { // 用户名不存在

this.$notify.error({

title: reslutData.data.message,

duration: 2000

});

return false;

}

if (reslutData.data.code != 200) { // 登录失败

this.$notify.error({

title: reslutData.data.message,

duration: 2000

});

return false;

} else { // 登录成功

// 弹框成功提示

this.$notify({

title: reslutData.data.message,

// message: '正在跳转中···',

type: 'success',

duration: 2000

});

console.log("token=" + reslutData.data.data.token)

// token写入本地客户端存储

localStorage.setItem("token",reslutData.data.data.token);

// token获取本地客户端存储

// var key = localStorage.getItem("token");

// console.log("key=" + key)

// 成功之后执行跳转  this.$router.replace

this.$router.replace('/search');

}

}.bind(this))

.catch(function (error) {

console.log("请求失败"+error);

});

},

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值