locked
WinJS.xhr returns immidiately RRS feed

  • Question

  • Hi,

    My call to the WinJS.xhr has no problem except when I call the same url twice one after the other. In that case, the first call is getting the data from the server but the second call returns immediately with the data of the first call without really launching and http request to the remote server. Any help is appriciated.

    BR,

    Mohammad 

    Tuesday, February 28, 2012 1:40 PM

All replies

  • Hi Mohammad,

    Can you share some real simple code that reproduces the problem?

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, February 28, 2012 3:00 PM
    Moderator
  • There are bunch of api that they all look like this:

    myApi.req = (function () { 
         var returnReqComplete; 
         var returnReqError; 
    
    function completeReq(request) { 
             var r = request.response; 
             var s = JSON.parse(r); 
             if (s.status == "SUCCESS") returnReqComplete(); 
             else returnReqError(s.error); 
         } 
      
    function errorReq(request) { 
             var r = request.response; 
             var s = JSON.parse(r); 
             returnReqError(s.error); 
         } 
    
    return { 
             call: function (number, address) { 
    
             WinJS.xhr({ url: "http://" + serverURL + "/_req/?number=" + number + "&address=" + address }).then(completeReq, errorReq); 
    
               return new WinJS.Promise(function (complete, error, progress) {returnReqComplete = complete; returnReqError = error;}); 
                 } 
             }; 
    })(); 
    
    They accept parameters and they return a promise to the caller. They also make a GET request to the server. When I call the api functions they reach the server the first time but subsequent calls return immediately with previous results. If I add a random parameter to the GET request (so that URL changes)  then the call always reaches the server.
    Tuesday, February 28, 2012 3:46 PM
  • My guess is that you are getting the cached results.  To analyze this further take fiddler trace:

    http://blogs.msdn.com/b/fiddler/archive/2011/09/14/fiddler-and-windows-8-metro-style-applications-https-and-private-network-capabilities.aspx

    If you issue the same request in IE 10 do you get the same behavior?  What headers are returned from your server?  Did you try sending a no-cache header?

    -Jeff


    Jeff Sanders (MSFT)

    Tuesday, February 28, 2012 4:02 PM
    Moderator
  • I am relatively positive that I get the cached result since it returns immediately with previous request data. My solution was to add a randomized parameter to  GET request for all URLs (i.e URL=URL + "&rand=" +randomstring) . That way I force WinJS.xhr to make a new request each time and the server simply ignores that parameter. This is obviously a hack. Is there any way that I invalidate WinJS.xhr cache?

    By the way, I think that this is a bug in WinJS.xhr implementation.

    Mohammad

    Tuesday, February 28, 2012 4:15 PM
  • Please answer the questions above, thanks.  You should get the cached results if the server is telling it to cache.


    Jeff Sanders (MSFT)

    Tuesday, February 28, 2012 4:21 PM
    Moderator
  • Check : http://msdn.microsoft.com/en-us/library/windows/apps/hh868281.aspx

    Friday, November 9, 2012 4:26 PM
  • As pointed by ZhanHuanyu,  you may set the header of request , in order to ensure that your responses are not cached or you need to send some kind of seed (a querystring with some time stamp which may make the url a unique one)  (which you mentioned in the above post) .

    At the same time we need to understand, why cached requests are equally important and it's definitely not a bug.
    Let's take a scenario, where the images are not urls but are binary data. you may not want to get the response each time from server for so many images for the tiles you navigate between the pages, and at the same time, if you are getting images for each time  from backend, you cannot keep them as static files.

    Hope this makes sense..... :)


    Arindam Basu

    Monday, November 12, 2012 10:32 AM