javascript-从WinJS.xhr响应获取URL

假设我有一个循环,其中在多个地址上调用WinJS.xhr().在响应处理程序中,是否可以确定从中处理响应的地址?是从xhr()传递给处理程序的XmlHttpRequest对象中识别它,还是手动传递其他内容?

我一直在浏览the documentation并在调试器中检查响应,但没有找到任何东西.

解决方法:

我不认为信息在回应中,但也不一定如此.每个xhr调用都有针对该特定调用返回的自己的Promise.我喜欢这样

//start with an array of some kind
var urls = [
    "http://something.com/1",
    "http://something.com/2",
    "http://something.com/3",
];

//map the array to a list of calls adding your url in so you have it
var results = urls.map(function(u) {
    return {url:u, response:WinJS.xhr({url:u})};
}

然后,您可以循环结果数组,然后获得URL.您可能希望将其包装在另一个Promise中,以使整个过程是异步的.

function xhrCallsAsync() {

    //start with an array of some kind
    var urls = [
        "http://something.com/1",
        "http://something.com/2",
        "http://something.com/3",
    ];

    //map the array to a list of calls adding your url in so you have it
    var results = urls.map(function(u) {
        return {url:u, response:WinJS.xhr({url:u})};
    }

    //return a Promise that completes when all of the Promises are complete
    return WinJS.Promise.join(results);
}

希望有帮助!

上一篇:javascript-如何在服务器上手动取消XMLHttpRequest文件上传(流)


下一篇:javascript-在Firefox中跨站点XmlHttpRequest?