我有一个 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 文件
请您参考如下方法:
IE有缓存AJAX请求的习惯。尝试这样的事情:
Date d = new Date();
req.open('GET', 'Config.xml?_' + d.getTime(), false);
这会将自 1970 年以来的毫秒数附加到每个请求,迫使 IE 获取新版本的 XML。