weixin_33726318 2019-03-12 18:08 采纳率: 0%
浏览 15

恢复中止的AJAX请求

During the abort, I am pushing the aborted object inside a array . When I call request again , I want to first call the aborted requests.

Current code:

    $.xhrPool = []
    var invokeService = function (method, url, params, data, headers = {}, 
    dataType = "json", contentType = "application/json") {
    return new Promise(function (resolve, reject, onCancel) {
        var xUrl = url + (!params ? '' :
            typeof (params) === "object" ? "?" + $.param(params) : "/" + params)
        var xhr = $.ajax({
            method: method,
            headers: { ...headers, "X-URL-Redirect": xUrl },
            dataType: dataType,
            contentType: contentType,
            url: "/api",
            data: typeof (data) === "string" ? data : JSON.stringify(data)
        })

        if ($.xhrPool.length !== 0 ) {
            for (var i = 0; i < $.xhrPool.length; i++) {
                if ($.xhrPool[i].readyState === 0) {
                    $.xhrPool[i] = xhr;
                    xhr.then(function success(data, status, req) {
                        console.log("call from insdie")
                        resolve(data)
                    }, function error(err, status) {
                        console.log(status, err.responseText)
                        reject(err.responseText || "Cannot conect to server");
                    })
                }
            }
        }
         xhr.then(function success(data, status, req) {
             resolve(data)
         }, function error(err, status) {
             console.log(status, err.responseText)
             reject(err.responseText || "Cannot conect to server");
         })

        onCancel(function () {
            if (xhr && xhr.abort) {
                xhr.abort();
                $.xhrPool.add(xhr);      
            }
            else console.log("could not abort request")
        });
    })
}

So when I call the invoke service again I want the $.xhrPool requests to run , and also update the $.xhrPool with only the aborted requests.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 求差集那个函数有问题,有无佬可以解决
    • ¥15 MATLAB动图问题
    • ¥15 【提问】基于Invest的水源涵养
    • ¥20 微信网友居然可以通过vx号找到我绑的手机号
    • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
    • ¥15 解riccati方程组
    • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
    • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
    • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
    • ¥50 树莓派安卓APK系统签名