jQuery - 在它返回的结果等于null之后停止AJAX调用

时间:2017-03-06 08:16:45

标签: javascript json ajax performance model-view-controller

      jQuery.ajaxSettings.traditional = true
function compareFilesResult(whlIDs) {
    var result = 0;
    $.ajax({
        url: '/ajax/CompareReturnAllResults',
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        dataType: "json",
        data: JSON.stringify({ _IDs: whlIDs }),
        success: function (data) {
            if (data != null) {
                compareAllData = data;
                return data;
            }
        },
        error: function (data) {
            return result;
        },
        fail: function (data) {
            return result;
        }
    });
}

现在我的Ajax总是被调用,即使结果为NULL,但我想成为NULL,如果它是NULL则再次停止调用它。

1 个答案:

答案 0 :(得分:1)

检查它是否为null然后返回如下:

jQuery.ajaxSettings.traditional = true
function compareFilesResult(whlIDs) {
var result = 0;
$.ajax({
    url: '/ajax/CompareReturnAllResults',
    contentType: "application/json; charset=utf-8",
    type: 'POST',
    dataType: "json",
    data: JSON.stringify({ _IDs: whlIDs }),
    success: function (data) {
        if (data == null) { /* try also: if (data === null) */
            return;
        }
        if (data != null) {
            compareAllData = data;
            return data;
        }
    },
    error: function (data) {
        return result;
    },
    fail: function (data) {
        return result;
    }
});
}