【问题标题】:XDomainRequest in IE- "fresh" data only in first callIE 中的 XDomainRequest - 仅在第一次调用时“新鲜”数据
【发布时间】:2014-11-01 21:14:13
【问题描述】:

我正在使用 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);                    
                  });                

            });
        }

谢谢! 史蒂夫

【问题讨论】:

    标签: javascript json internet-explorer refresh xdomainrequest


    【解决方案1】:

    如果您想确保不会从 ajax 调用中获取缓存信息,您可以将时间戳附加到 url。

    例如:

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

    【讨论】:

    • 我尝试了您的建议,但在这种情况下,它使 XDR 请求返回“无法识别的关键字”错误并且没有返回数据。需要明确的是,这在 Chrome 或 Firefox(使用 JQuery JSON 请求选项)中不是问题。
    • 好的,这是网络服务的问题。通常时间戳不应该是一个问题。很抱歉我不能帮助你。也许唯一的其他解决方案是创建一个代理服务,它将请求转发到真正的 Web 服务。在对代理服务的请求中,您可以传递时间戳参数。此外,您不必为 IE 使用解决方法,因为您不必再​​使用跨域 ajax 请求。
    • 我做了一些后续工作,并将您的原始答案标记为正确。虽然 USGS 服务器不会像您建议的那样接受附加参数,但它确实有一个有效的开始时间参数。我最终在 Epoch 中获取当前日期/时间,从中减去 3 小时,然后将其作为开始日期/时间传递。现在我的 URL 始终是唯一的,因此在 IE 中返回非缓存信息。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2012-09-17
    相关资源
    最近更新 更多