在IE-"新鲜"中的XDomainRequest数据仅在第一次通话中

时间:2014-09-08 15:33:12

标签: javascript json internet-explorer refresh xdomainrequest

我正在使用JSON请求在我正在开发的网页中检索一些流量计信息。对于IE兼容性,我使用的是XDomainRequest。 XDR在第一次加载页面时成功检索数据,但后续调用(我在加载后在页面上使用windows.setInterval)不返回更新的信息。清除浏览器缓存也不会做任何事情。页面加载新数据的唯一方法是实际重启IE并加载页面。我错过了什么?

这是我的XDR代码:

        //Now Access & Process the NWS Gage Data
        if ($.browser.msie && window.XDomainRequest) {
           //Internet Explorer Doesn't support JQuery's getJSON so you must use an alternative method
           // Use Microsoft XDR
           var xdr = new XDomainRequest();
           xdr.open("get", "http://waterservices.usgs.gov/nwis/iv/?format=json&sites=12150800,12167000,12161000,12150400,12138160,12134500,12186000,12155300,12155500&parameterCd=00065");
           xdr.onload = function () {
           //parse response as JSON
           var JSON = $.parseJSON(xdr.responseText);
           if (JSON == null || typeof (JSON) == 'undefined')
           {
                JSON = $.parseJSON(data.firstChild.textContent);
           }
              array.forEach(JSON.value.timeSeries, function(curGage, i){
                curGageID = curGage.sourceInfo.siteCode[0].value;
                curGageName = curGage.sourceInfo.siteName;
                curGageHeight = curGage.values[0].value[0].value;
                curGageObs = curGage.values[0].value[0].dateTime;
                //Another Internet Explorer quirk. Date format must be changed due to IE's different interpretation
                curGageObs = curGageObs.replace(/\-/g,"/");
                curGageObs = curGageObs.replace(/T/g," ");
                curGageObs = curGageObs.substring(0,curGageObs.length-10);
                var theDate = new Date(curGageObs);

                document.getElementById('u' + curGageID + 'Height').innerHTML = curGageHeight + " Feet";
                document.getElementById('u' + curGageID + 'Date').innerHTML = theDate.toString('M/d/yyyy @ h:mm tt');
                assignNwsStageColor(curGageID, curGageHeight);                    
              });  
           };
            xdr.send();
        } else {           
            $.getJSON("http://waterservices.usgs.gov/nwis/iv/?format=json&sites=12150800,12167000,12161000,12150400,12138160,12134500,12186000,12155300,12155500&parameterCd=00065", function(data) {                
                  array.forEach(data.value.timeSeries, function(curGage, i){
                    curGageID = curGage.sourceInfo.siteCode[0].value;
                    curGageName = curGage.sourceInfo.siteName;
                    curGageHeight = curGage.values[0].value[0].value;
                    curGageObs = curGage.values[0].value[0].dateTime;
                    var theDate = new Date(curGageObs);

                    document.getElementById('u' + curGageID + 'Height').innerHTML = curGageHeight + " Feet";
                    document.getElementById('u' + curGageID + 'Date').innerHTML = theDate.toString('M/d/yyyy @ h:mm tt');
                    assignNwsStageColor(curGageID, curGageHeight);                    
                  });                

            });
        }

谢谢! 史蒂夫

1 个答案:

答案 0 :(得分:1)

如果您想确保不从ajax调用中获取缓存信息,可以在网址上附加时间戳。

例如:

$.getJSON("http://example.com?param1=asdf&_=" + new Date().getTime());