进来天气十分炎热,很难静下心来开发新东西,就找到之前写的一个线索收集分配的小系统,进行整理优化。
在登录页面,之前是直接使用jq的ajax进行提交数据,发现pearadmin封装的有http.js 是可以代替直接使用ajax的,所以就进行了改造。
先上原来的代码
$.ajax({
type: 'post',
url: '<?=site_url("/user/user/checkuser");?>',
//headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
data: data.field,
success: function(r){
if(r.code==0){
layer.msg('验证通过 '+r.uname+' 欢迎进入!',{icon: 1});
setTimeout(function () {window.location.href = "<?=site_url('/home');?>"; }, 2000);
}else if(r.code==2){
layer.msg('验证码填写错了', {icon: 5});
$("#codeImage").attr("src", '<?=site_url("login/captcha");?>?' + Math.random());
} else{
layer.open({
type: 0,
offset: 'auto',
anim: 6,
time: 5000,
closeBtn: 0,
title: ['错误提示', 'font-size:15px;color:red;'],
content: r.msg,
});
}
}
改造后的代码
http.ajax({ url: '/manage.php/login/check', method: 'POST',data:JSON.stringify(data.field)})
.done(function (data) {
if (data.code === 0) {
form.val(http.getQueryVariable('tableId'), data.data);
} else {
popup.failure(data.msg);
}
})
.fail(function (data) {
console.log(data)
popup.failure('服务器错误');
});
感觉差距不是很大啊,但是仔细研究之后发现,改造前的代码没有改造后的代码考虑的方面多,安全性有所提高。
官方文档上,也没有http模块的使用方法,我就把自己摸索的记录一下:
先引入 http模块
在layui.use中增加 'http', 并 var http=layui.http;
在需要的地方使用
http.ajax() 中参数说明如下:
url 提交的地址
method 提交方式 POST 或 GET 等
data 提交的参数,需要进行json编码哦(若是采用get,可以将参数组装到url上)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69992267/viewspace-2902688/ ,如需转载,请注明出处,否则将追究法律责任。