0

I have a xml file that changed when i update some values, and i'm trying to read it from javascript in asp.net.

But once i run the project, every time i try to read the xml file, the file is the same from the begining...

this is my javascript code that i have in server side

script = "function OnClientDragEnd(dock, args)" +
                        "{" +                    
                        "   req = false; " +
                        "   var isIE = false;" +
                // branch for native XMLHttpRequest object
                        "   if(window.XMLHttpRequest && !(window.ActiveXObject)) {" +
                        "       try {" +
                        "           req = new XMLHttpRequest();" +
                        "       } catch(e) {" +
                        "           req = false;" +
                        "       }" +
                // branch for IE/Windows ActiveX version
                        "   } else if(window.ActiveXObject) {" +
                        "       try {" +
                        "           req = new ActiveXObject('Msxml2.XMLHTTP');" +
                        "       } catch(e) {" +
                        "           try {" +
                        "               req = new ActiveXObject('Microsoft.XMLHTTP');" +
                        "           } catch(e) {" +
                        "               req = false;" +
                        "           }" +
                        "       }" +
                        "   }" +
                        "   if(req) {" +
                        "       req.onreadystatechange = function(){processReqChange(dock,args)};" +
                        "       req.open('GET', 'Config.xml', false);" +
                        "       req.send('');" +
                        "   }" +
                        "}" +
                        "function processReqChange(dock,args) {" +
                            // only if req shows "loaded"
                        "   if (req.readyState == 4) {" +
                                // only if "OK"
                        "       if (req.status == 200) {" +
                                // ...processing statements go here...
                        "           var iiii = req.responseXML.getElementsByTagName('Object');alert(iiii.length);" +//Value stays the same after xml have changed
                        "       } else {" +
                        "           alert('There was a problem retrieving the XML data: ' + req.statusText);" +
                        "       }" +
                        "   }" +
                        "}";
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "PositionChanged", script, true);

This code is in http://developer.apple.com/internet/webcontent/xmlhttpreq.html

What do i need to do for get always the xml file updated

SlimBoy
  • 311
  • 2
  • 4
  • 14

1 Answers1

2

IE has a habit of caching AJAX requests. Try something like this:

Date d = new Date();
req.open('GET', 'Config.xml?_' + d.getTime(), false);

this will append the number of milliseconds since 1970 to each request, forcing IE to fetch a new version of the XML.

Mark Kinsella
  • 205
  • 1
  • 5
  • @SlimBoy: No, that would be the result of some surrounding code. – Jørn Schou-Rode Feb 17 '10 at 19:09
  • +1. This should do the trick. A few alternatives are available here: http://stackoverflow.com/questions/367786/prevent-caching-of-ajax-call – Jørn Schou-Rode Feb 17 '10 at 19:10
  • I think that to get the date is this way, i'm trying to see if this works var ui = new Date(); req.open('GET', 'Config.xml?_'+ui.getDate(), false); – SlimBoy Feb 17 '10 at 19:50
  • Sorry but i make a mistake, this is the corret answer!! its getTime and not getDate like my last comment.. Thanks for the answer!!! var ui = new Date(); req.open('GET', 'Config.xml?_'+ui.getTime(), false); – SlimBoy Feb 17 '10 at 20:04
  • @Mark Kinsella: +1, but using `'Config.xml?_' + (+new Date())` would make your code one line shorter ;-) – Andy E Feb 18 '10 at 11:09