试图读取XML文件,但他总是一样的

时间:2010-02-17 18:36:07

标签: asp.net javascript xml

我有一个更新某些值时更改的xml文件,我正在尝试从asp.net中的javascript中读取它。

但是一旦我运行该项目,每次我尝试读取xml文件时,该文件都是从开头开始的......

这是我在服务器端的javascript代码

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

此代码位于http://developer.apple.com/internet/webcontent/xmlhttpreq.html

我需要做什么才能获得更新的xml文件

1 个答案:

答案 0 :(得分:2)

IE习惯于缓存AJAX请求。尝试这样的事情:

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

这会将自1970年以来的毫秒数附加到每个请求,迫使IE获取新版本的XML。