javascript - 从 WinJS.xhr 响应获取 URL

标签 javascript xmlhttprequest windows-store-apps winjs

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

我一直在浏览 the documentation以及检查调试器中的响应,但未能找到任何内容。

最佳答案

我不认为响应中包含该信息,但也不需要。每个 xhr 调用都有自己的 Promise,该 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 - 从 WinJS.xhr 响应获取 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14047892/

相关文章:

c# - 我可以将应用程序上传到 Windows 应用商店而不使用 .net native 工具链进行编译吗?

javascript - 访问 JavaScript 中 some() 函数中使用的对象

javascript - 如果电子表格中有图像,为什么 XMLHttpRequest 无法在 Firefox 中发送 excel 文件?

javascript - 如何更改元素上的关键帧位置

cookies - 子域上的第三方代码

asynchronous - 如何在 Angular 2 中发出简单的 JSONP 异步请求?

javascript - Windows 8.1 应用程序中应用程序数据和 session 数据之间的差异

c# - 如何在 Windows 应用程序中为歌曲设置专辑封面(例如,没有 tag-lib#)?

javascript - 全部替换为特殊字符在 Javascript 中不起作用

javascript - Backbone : Can't remove a view when call method remove()