views:

58

answers:

1

Hi!

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

+2  A: 

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
Any reason why this is giving me an error of missing ';'?
SlimBoy
@SlimBoy: No, that would be the result of some surrounding code.
Jørn Schou-Rode
+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
I think that to get the date is this way, i'm trying to see if this worksvar ui = new Date();req.open('GET', 'Config.xml?_'+ui.getDate(), false);
SlimBoy
No, it still dont update the xml file
SlimBoy
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
@Mark Kinsella: +1, but using `'Config.xml?_' + (+new Date())` would make your code one line shorter ;-)
Andy E