AjaxSetup与jqgrid冲突

时间:2015-05-07 22:53:45

标签: javascript jquery ajax jqgrid

我想将ajaxsetup用于我的所有ajax调用,但似乎它干扰了我网站上的jqgrids。有没有办法全局设置我的ajax调用,而不会弄乱jqgrids内部调用。以下代码是我想在除jqgrids

之外的ajax调用中使用的代码
$.ajaxSetup({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    error: function(xhr, status, error) {
        alert(xhr.responseText);
    }
})

1 个答案:

答案 0 :(得分:1)

不要修改全局默认值,而是编写自己的AJAX函数,该函数合并默认值并调用$.ajax。然后在代码中使用此函数代替$.ajax

var myAjaxSettings = {
    type: "POST",
    contentType: "application/json; charset=utf-8",
    error: function(xhr, status, error) {
        alert(xhr.responseText);
};
function myAjax(options) {
    options = $.extend({}, options, myAjaxSettings);
    return $.ajax(options);
}

我保持这个定义简单,它不接受$.ajax所做的所有不同的参数格式,只接受一切在options个对象参数中的格式。如果你愿意,你可以更详细。

您可以像$.ajax

一样使用它
myAjax({
    url: "yourURL",
    data: {
        param1: value1,
        param2: value2
    },
    success: function(response) {
        alert(response);
    }
});