WinJS检查xhr请求是否被取消

时间:2014-02-27 07:26:55

标签: windows-8 winjs

我的xhr请求如下,

var promise=[];
promise[0]=.WinJS.xhr({
        type: "POST",
        url: "some url",
        data: somedata,
        headers: { "Content-type": "application/json; charset=utf-8" },
        //  responseType: "json"
    }).then(function(success){}, function(error){});

promise[1]=.WinJS.xhr({
        type: "POST",
        url: "some url",
        data: somedata,
        headers: { "Content-type": "application/json; charset=utf-8" },
        //  responseType: "json"
    }).then(function(success){}, function(error){});
promise[2]=...
.
.
.

现在我将取消一些例如promise [0],promise [5],

的请求
promise[0].cancel();
promise[5].cancel();

现在加入我想知道哪些请求被取消,

WinJS.Promise.join(promise).done(function(xhr){
//here i should be able to check which requests are cancelled
});

1 个答案:

答案 0 :(得分:0)

promise对象有第二个参数,您可以在其中传递将触发onCalcel的函数。

var aPromise = new WinJS.Promise(init, onCancel);

WinJS.xhr功能以一种不同的方式工作。

取消操作时会调用错误函数,以及下载操作期间可能发生的任何错误。

xhrPromise = WinJS.xhr({ url: "<your choice of URL>" })
    .then(function complete(result) {
        //SUCCESS
    }, 
    function error(result) {
        if (result.name == "Canceled") {
            //CANCELED
        }
        else {
            //OTHER ERRORS
        } 
    });

所以如果在错误函数中你返回一个特定值,当它是“取消”时,你会在连接中的所有承诺完成后找到这个值。