Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting URL from WinJS.xhr Response

Suppose I have a loop in which I call WinJS.xhr() on multiple addresses. In the response handler, is there a way to determine the address from which the response is being handled? Either by discerning it from the XmlHttpRequest object that xhr() passes to the handler or by passing something else in manually?

I've been looking through the documentation as well as examining the response in the debugger but haven't been able to find anything.

like image 786
David Avatar asked Nov 24 '25 12:11

David


1 Answers

I don't think that information is in the response, but it doesn't need to be. Every xhr call has its own Promise that is returned for that specific call. I like to do it this way...

//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})};
}

And then you can loop the results array and you have the url. You might want to wrap that in another promise so the whole thing is asynchronous.

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);
}

Hope that helps!

like image 111
Jeremy Foster Avatar answered Nov 27 '25 03:11

Jeremy Foster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!