locked
How to disable caching in a winjs.xhr request

    Question

  • Hi,

    I want to disable caching in a winjs.xhr request so that the latest updated data will be fetched and populated in a winjs.ui.listview when navigating between pages.

    My scenario is as follows.

    Page 2 contains a list view with data and there is a certain flag which has to be manipulated by the user. It can be the same or different. The manipulation (updating) of the data is done on the 4th page and then it returns back to Page 2. On loading this page 2 the xhr connects to the REST web service and pulls the data. I notice that the latest data is not being refreshed by this request and displayed in the list view. It always shows the same cached data that was displayed when the application loaded for the first time.

    What can I do to overcome this limitation and ensure that the xhr request will ALWAYS fetch the latest data from the backend database and display it correctly in the winjs.ui.listview.

    Thanks,

    Rajesh.

    Wednesday, December 4, 2013 10:25 AM

Answers

  • See if the following works for you:
    var nocache = new Date().getTime(); 
    var path = 'http://localhost:<some-port>/<function>/?cache=' + nocache;
    the querystring name could be anything, values determine the request
    This is not unsafe but it's not advisable, you will have to use it for diagnostics only to figure out the real reason.

    If you could use jQuery for XHR then its as simple as:
    $.ajaxSetup({ cache: false });

    jQuery 2.0 works well with Windows 8 apps as long as you are not manipulating DOM too much dynamically.


    Thursday, December 5, 2013 10:14 AM

All replies

  • Hi Rajesh,

    Are you trying to find this: How to ensure that WinJS.xhr resends requests ? Add a If-Modified-Since might be the solution.

    WinJS.xhr({ 
        url: "http://www.microsoft.com",
        headers: {
            "If-Modified-Since": "Mon, 27 Mar 1972 00:00:00 GMT"
        } })
        .done(function complete(result) {
           ...
    });

    --James



    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.


    Wednesday, December 4, 2013 11:36 AM
    Moderator
  • Hi,

    I saw that snippet but it doesn't do anything at all. What could be the reason. And I read in other forums that it works for some and does not work at all for others.

    And even tried this Cache-control but that also didn't work.

    Regards,

    Rajesh.

    Wednesday, December 4, 2013 11:42 AM
  • Hi Rajesh,

    Well, for your scenario, can you validate when the page 2 is navigated from page 4, the correct flag value has been passed? And try to use fiddler to find out if the correct data been sent and correct data received.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Thursday, December 5, 2013 8:48 AM
    Moderator
  • Hi James,

    Thanks for your response. Let me explain what is happening with the help of some certain sql queries

    The query is as follows: Select employeenumber, firstname, lastname, designation, status from employee.

    employeenumber, firstname + lastname, designation and status is displayed in the listview in 4 lines. the value of the column status can be active or inactive.

    First time : - the value of the column status for all the employees are active.

    All the fields show as Active in the listview on page 2.

    selecting an item navigates to page 3 and then page 4 where the user can change the status of the selected employee.

    On page 4, the query  is as follows, update employee set status = 'Inactive' where employeenumber = 500 (example)

    On completion of this page and navigating back to page 2 this updated data should be visible in the list view. But the navigation happens so fast and the listview is instantly populated and there is no delay in displaying the data.The status shows as Active only everytime no matter how many times I repeat this cycle.

    But when the page 2 loads for the first time ,there is a slight delay before populating the list view.

    Thanks,

    Rajesh.

     

    Thursday, December 5, 2013 9:13 AM
  • You can try the following:
    - add a randomly generated string at the end of the url (I normally use DateTime.Now.TimeOfDay.ToString() to achieve this, you will have to find a javascript equivalent). This is known as Cache buster
    - check the "Navigated" event, add the XHR request there to see if firing a fresh request indeed helps
    - use fiddler to see the actual request going back to your server
    - the fact that you don't see any percieved delay while navigating back tells me that XHR may not be fired at all
    - to be on a safer side, ensure that update query is correctly sent to the database, and the update is successful, its best to write an integration test in such scenario to ensure that database operations are correct
    Thursday, December 5, 2013 9:48 AM
  • Hi Sanjay,

    Can you please give an example on how to add the random generated string. What will be the syntax of the xhr request. Is it a query string or just a concatenation. And how will it guarantee that the return results will not be affected.

    I believe that trying this out will result in a security violation of the http request. I remember trying out this before and got a warning about the response.request and it was malformed.

    Thanks,

    Rajesh.

    Thursday, December 5, 2013 10:03 AM
  • See if the following works for you:
    var nocache = new Date().getTime(); 
    var path = 'http://localhost:<some-port>/<function>/?cache=' + nocache;
    the querystring name could be anything, values determine the request
    This is not unsafe but it's not advisable, you will have to use it for diagnostics only to figure out the real reason.

    If you could use jQuery for XHR then its as simple as:
    $.ajaxSetup({ cache: false });

    jQuery 2.0 works well with Windows 8 apps as long as you are not manipulating DOM too much dynamically.


    Thursday, December 5, 2013 10:14 AM