开发者

Why do $.getJSON and $.ajax both return immediately in IE?

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").开发者_如何学Ctoggle();
                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);



        }
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜