我所常用的ajax调用格式

时间:2021-04-11 21:19:11

ajax:

$.ajax({
    type: "post",
    datatype: "json",
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    url: "/ProductTypeAndCat/AddType",
    async: true,
    cache: false,
    data: { typeName: typeName },
    success: function (data) {
        if (data.F == 1) {
            alert(data.M);
            return;
        }

alert("添加成功!");
        location.href = "/Admin/ProductType";
    },
    error: function (xhr, status, error) {
        alert(error);
    }
});

form提交,利用此插件(http://jquery.malsup.com/form/):

$('#frmDemo').ajaxSubmit({
    type: "post",
    url: "/ProductTypeAndCat/AddCategory",
    success: function (data) {
        if (data.F == 1) {
            alert(data.M);
            return;
        }

alert("添加成功");
        location.href = "/admin/createProductCategory";
    },
    error: function (xhr, status, err) {
        alert(err);
    }
});