九十九:CMS系统之ajax提交修改密码的数据

顺便实现ajax提交的时候默认提交csrf_token

var http = {
'get':function (args) {
args['method'] = 'get';
this.ajax(args);
},
'post':function (args) {
args['method'] = 'post';
this.ajax(args);
},
'ajax':function (args) { // 将头部信息放到请求
this._ajaxSetup();
$.ajax(args);
},
'_ajaxSetup':function(){ // 将csrftoken放到头部信息
$.ajaxSetup({
'beforeSend': function (xhr, settings) {
if(!/^(GET|HEAD|OPTIPNS|TRACE)$/i.test(settings.type) && !this.crossDomain){
var csrf_token = $('meta[name=csrf_token]').attr('content'); // 获取csrf_token
xhr.setRequestHeader('X-CSRFToken', csrf_token)
}
}
});
}
};

修改密码页的js

$(function () {
$('#submit').click(function (evnet) {
evnet.preventDefault(); //阻止默认的提交表单事件

var oldpwd = $("input[name='oldpassword']").val();
var newpwd = $("input[name='oldpassword']").val();
var newpwd2 = $("input[name='oldpassword2']").val();

ajax.post({
'url': '/cms/resetpwd/',
'data': {
'oldpwd': oldpwd,
'newpwd': newpwd,
'newpwd2': newpwd2,
},
'success': function (data) {
console.log(data);
},
'fail': function (error) {
console.log(error);
}
})
});

});

猜你喜欢

转载自www.cnblogs.com/zhongyehai/p/11914664.html