axios(Vue中的ajax请求)

这篇博客介绍了如何在Vue项目中导入和使用axios库进行AJAX请求。首先,通过import导入Vue、axios和qs模块,然后全局注册axios,使其在Vue实例中可直接调用。接着,在具体的组件如login.vue中,可以使用this.axios发送HTTP请求。
在js中

//导入
import Vue from ‘vue’
import axios from ‘axios’
import qs from ‘qs’

//全局注册,使用方法为:this.axiosVue.prototype.axios Vue.prototype.axiosVue.prototype.axios = axios

//添加
Vue.use(axios);
在这里插入图片描述

在login.vue中使用

//发送axios请求 例如

var _this = this;
//验证
this.$refs.ruleForm2.validate((valid) => {
    if (valid) {
        //_this.$router.replace('/table');
        this.logining = true;
        //NProgress.start();
        var loginParams = {name: this.ruleForm2.account, pwd: this.ruleForm2.checkPass};

        /*//测试axios
        _this.$http.get("/test").then(data=>{
           console.debug("ssss")
        });*/

        //模拟登陆 
        //发送axios请求
        //获取后台响应data信息
        _this.$http.post("/user/user/login",loginParams).then(data=>{
            this.logining = false;
            //NProgress.done();
            let {success, message, obj} = data;
            var p = data.data;
            console.debug(p.success);
	//判断为false 则登录成功
            if (!p.success) {
                this.$message({
                    message: "账户或密码错误",
                    type: 'error'
                });
            } else {
                console.debug("uuuuuuuu");
                //设置值
                obj={"name":"xz"};
                //类似于session
                sessionStorage.setItem('user', JSON.stringify(obj));
                this.$router.push({path: '/table'});
            }
        })
    } else {
        console.log('error submit!!');
        return false;
    }
});
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值