使用ajax实现登录业务

来源:互联网 发布:关系型数据库基本特征 编辑:程序博客网 时间:2024/06/04 22:34

上周用angular做了登录和注册业务,但是因为里边有许多插件,比较复杂,所以这里将其抽离出来,希望对大家有所帮助。
废话不多说,直接上代码。

/*这里不做加密处理,如有需要可自行加密账户和密码*/var number = $.trim($("#number").text()).toUpperCase();var password = $.trim($("#password").text());/*登录*/var promise,    data;$.ajax({    url: "1.json",//接口地址    data: {        logintype: 1,//登录类型(1、身份证号加密码登录2、手机号加密码3、手机号加验证码)        usertype: 1,//用户类型(1、个人2、单位)        number: number,//用户名        password: password,//密码    },    type: "POST",    dataType: "json",    success: function(response){        console.log("个人登录", response);        promise = new Promise(function(resolve,reject){            data = response;            resolve();        });        return promise;    }}).then(function(){    if (data.IsOK) {        alert("登录成功!");        var ticket = data.Result;//token值        logoObj.sendLogin(ticket);//根据票据信息获取个人信息    }else{        alert("登录失败");    };})/*登录业务*/var logoObj = {    sendLogin: function(ticket){        $.post({            url: "接口地址",            dataType: "json",            data: {                ticket:ticket            }        }).then(function(res){            if(res.IsOK){                window.localStorage.setItem('denglu',window.JSON.stringify(res.Result));                location.href='index.html#/tab/PersonModule';//跳转到个人信息页面                console.log(response.Result);            }else {                alert("登录失败");            }        })    }};

以上就是登录的相关业务,具体的加密方式这里不做说明,如有需要大家可以和后台人员进行交流确定。