代码之家  ›  专栏  ›  技术社区  ›  scottm

为什么$.getJSON和$.ajax在IE中都会立即返回?

  •  0
  • scottm  · 技术社区  · 16 年前

    我试图发出一个异步请求,从我的服务器获取一些数据。这一切在Firefox中都能很好地工作,但在InternetExplorer中,在收到任何数据之前,会立即调用回调。

    $.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);
    
    
    
            }
    });
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   davidtbernal    16 年前

    数组存在长度属性,但对象不存在长度属性。例如,在firebug控制台中,您可以键入:

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

    然后使用数组进行尝试:

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


    答复评论: $.each 在空对象上没有问题。你可以留一个柜台。
                var resultCount=0;
    
                $.each(data, function(i, item){
                                resultCount++;
                                //...
    
                });
    
               if(resultCount == 0){
                        //error
                        return false;
    
                }
    

    尽管如此,我不确定这是否真的是你的问题(但一定要检查一下,因为它很可能是)。你试过了吗 alert data 在IE里?有时,您只需不断移动警报,直到发现问题。