views:

62

answers:

1

I am trying to make an asynchronous request to get some data from my server. This all works perfectly well in Firefox but in Internet Explorer, the callback is being called immediately, before any data is received.

$.ajax({
    url: "charts.php", 
    data: { site: site, start: toDateString(start), end: toDateString(end) },
    cache: false,
    dataType: "json",
    success:
    function(data) {

      var dataPoints = [];

      if(data.length == 0){
       $("#error").children("label").eq(0).html("There were no results for the site and range selected.");

       if($("#error").css("display") == "none"){
        $("#error").toggle();
       }

       $("#large-loader").toggle();
       return false;

      }


      //add each pair of time/maxcalls to an array
      $.each(data, function(i, item){
        var minute = item[0];

        dataPoints.push({
         label: pad(parseInt(minute / 60)) + ":" + pad((minute%60)),
         data: [minute, item[1]]
        });
      });

      var options = {
       xaxis : {
        ticks : 24,
        tickSize: 60
        },
       legend : {
        show: true,
        margin: 10,
        backgroundOpacity: .3
        },
       grid: {
        hoverable: true
        }
      };


      //hide loader, show chart
      $("#large-loader").toggle();

      $("#chart-container").toggle();
      $.plot($("#chart"), [data], options);



     }
});
A: 

The length property exists for arrays, but not for objects. E.g., in your firebug console, you can type:

var a={'a':'e','b':'c'};
a.length==undefined;//true

Then try it with an array:

var a=['a', 'b','c','d'];
a.length;//4


In reply to the comments: You ought to be able to do $.each on the empty object without problem. You can keep a counter, e.g.

            var resultCount=0;

            $.each(data, function(i, item){
                            resultCount++;
                            //...

            });

           if(resultCount == 0){
                    //error
                    return false;

            }

That being said, I'm not sure if this is really your problem (but do check, because it well could be). Have you tried alerting data in IE? Sometimes you just have to keep moving alerts around until you find the problem.

notJim
if that's the case, how do i tell if the data object contains anything? also, that expression is evaluating to true ...
scottm
it's possible his json result actually contains a length attribute
Matt
@Matt, I haven't explicitly defined one
scottm