假设我有一个循环,我在其中对多个地址调用 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/