如何合并两个要在AJAX响应中使用的对象?

时间:2016-06-16 14:28:38

标签: javascript jquery ajax xmlhttprequest

我有一个问题,我在使用XHR请求,但它在IE8中破坏了。我希望提取XHR调用和AJAX调用,合并它们并将它们添加到$ .ajax()函数中,如下所示:

var loadingXHR = {
    xhr: function () {
        var xhr = new window.XMLHttpRequest;
        xhr.addEventListener('progress', (function (evt) {
            var parentWidth, percent, percentComplete, width;
            if (evt.lengthComputable) {
                width = loadEl.width();
                parentWidth = loadEl.offsetParent().width();
                percent = 100 * width / parentWidth;
                percentComplete = Math.max((evt.loaded / evt.total) * 100, percent);
                loadEl.stop().animate({
                    width: Math.min(percentComplete, 100) + "%"
                }, 120);
            }
        }), false);
        return xhr;
    }
}

var ajaxResponse = {
    type: "POST",
    url: url,
    dataType: "json",
    cache: false,
    contentType: "application/json; charset=utf-8",
    data: data,
    error: function (xhr, textStatus, errorThrown) {
       /* error */
    },
    success: function (model) {
      /* success */
    }
}

if (conditional) {
    return $.ajax($.extend(loadingXHR, ajaxResponse));
}

return $.ajax(ajaxResponse);

然而,这个选项并不起作用,我唯一的解决方案是创建两个不同的AJAX调用并将其包装在条件中。

0 个答案:

没有答案