简体   繁体   English

带有JSONP的jQuery.getJSON为不同的调用返回相同的数据

[英]jQuery.getJSON with JSONP is returning the same data for different calls

I'm using jQuery.getJSON() to hit the same web service three times in a row with different parameters. 我正在使用jQuery.getJSON()连续三次使用相同的参数访问同一Web服务。 Then I'm drawing charts with the data I receive. 然后用接收到的数据绘制图表。 The problem though is quite basic, it doesn't have to do with the chart at all. 问题虽然很基本,但与图表完全无关。 When I get the data in my callback function it is not consistently the "right" data. 当我在回调函数中获取数据时,它并不一定是“正确的”数据。 For example: 例如:

var URL1 = http://mysite.com/myAPI/metrics?type=pageloads&date=02022012&callback=?
var URL2 = http://mysite.com/myAPI/metrics?type=formsubmissions&date=02232012&callback=?
var URL3 = http://mysite.com/myAPI/metrics?type=uniqueusers&date=02022012&callback=?

var getDataAndDraw = function(metricURL, chartDiv) {
    $.getJSON(metricURL, function(data){
        console.log(data.metricName);

        // i do my charting here
    });
};

getDataAndDraw(URL1, 'pageloadsDiv');
getDataAndDraw(URL2, 'formsubmissionsDiv');
getDataAndDraw(URL3, 'uniqueuserDiv');

Sometimes I get the right 3 graphs and the console displays the expected metric names. 有时我会得到正确的3个图,并且控制台会显示预期的度量标准名称。 But sometimes I get some mix of the same ones. 但是有时候我会混合使用一些相同的东西。 For example... I'll be displaying the pageloads metrics twice and uniqueuser metrics once and console will show that the data for pageloads was returned twice and uniqueuser once. 例如,我将显示两次页面加载指标,一次显示uniqueuser指标,控制台将显示两次返回页面加载数据,并且一次显示uniqueuser。

Any ideas why this would be happening? 任何想法为什么会这样?

The AJAX requests are likely being cached. AJAX请求可能被缓存。 This code should fix that problem. 此代码应解决该问题。

$(document).ready(function() {
  $.ajaxSetup({ cache: false });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM