通用Ajax

function Ajax(url, data, success, type, dataType, error) {
    this.url = url;
    this.data = typeof data == "string" ? $(data).serialize() : data;
    this.success = success;
    this.type = type || "post";
    this.dataType = dataType || "json";
    this.error = error || function () { showErr('系统繁忙,请稍候再试'); };
}

$.extend(Ajax.prototype, {
    _ajax: function () {
        $.ajax({
            url: this.url,
            type: this.type,
            dataType: this.dataType,
            data: this.data,
            success: this.success,
            error: this.error
        });
    },
    post: function (dt) {
        this.type = "post";
        this.dataType = dt || "json";
        this._ajax();
    },
    get: function (dt) {
        this.type = "get";
        this.dataType = dt || "json";
        this._ajax();
    },
    ajax: function () {
        this._ajax();
    }
});
$("#submit").on('click', function () {
  if($(this).attr('submiting') == "1") return;
	var valiResult = validate();
	if (!valiResult) {
		return;
	}
	$(this).attr('submiting', "1");
	formSubmit();
});

function validate() {
	var moblie = $("#username");
	var passwd = $("#password");
	var f1 = notEmpty(moblie),
		f2 = notEmpty(passwd);
	return f1 && f2;
}

var formSubmit = function () {
	var data = {
		UserName: $("#username").val(),
		Password: $("#password").val(),
		Remberme: $("#rem").prop("checked")
	};
    new Ajax("/Login/Login", data, function (rep) {
       //处理返回数据 
    }).post(); 
}

  

 

posted @ 2016-08-10 14:28  风吹过春天  阅读(212)  评论(0编辑  收藏  举报