假设我有一个循环,我在多个地址上调用WinJS.xhr()
。在响应处理程序中,有没有办法确定处理响应的地址?通过从xhr()
传递给处理程序的XmlHttpRequest对象中辨别它还是通过手动传递其他内容来进行辨别?
我一直在查看the documentation以及检查调试器中的响应但无法找到任何内容。
答案 0 :(得分:4)
我认为信息不在回复中,但并不一定如此。每个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})};
}
然后你可以循环结果数组,你就有了网址。您可能希望将其包装在另一个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);
}
希望有所帮助!