1

我正在尝试使用函数将 a 嵌入到调用中,$.post但是当数据发送到我的回调函数时出现以下错误:$.ajaxsetTimeout

TypeError: data.sector is undefined
for (var i = 0, len = data.sector.length; i < len; i++) {

任何帮助都是解决方案,非常感谢。

阿贾克斯的东西:

        var browserUTC = new Date().getTime();
        var test = Math.floor(browserUTC / 1000);
        //document.write(test);

        $.ajax({
            type : 'POST',
            url  : 'php_scripts/currentFile.php',
            dataType : 'json',
            data     : { "func" : "getFirstAndNext", epochTime : browserUTC, "func" : "getUpdateAndInterval" },
            success  : function( data ) {

            data1 = JSON.parse(data.first);
            next = JSON.parse(data.next);
            sector_callback( data1 );

            console.log(data.interval);
            console.log(data.update);

            setTimeout(function () {

                // ajax to retreive new file
                $.post ('php_scripts/newFile.php',
                {
                    epochTime : data.next,
                    dataType : 'json',
                    data : { "func" : "getNext", epochTime : data.next },
                }),
                removeSectors();
                next = JSON.parse(data.next);
                console.log('hello');
                console.log(next);
                sector_callback( next );
                //console.log('worked');

                console.log("Ding!",Date()); // This is where I will load first JSON file
                setInterval(function () { console.log("Dong!",Date()); }, data.interval * 60 * 1000); 
            }, data.update);    

            },
            error : function ( XMLHttpRequest, textStatus, errorThrown ) {
            }
        });

打回来:

    var allPolygons = [];
    function sector_callback(data) {
        //console.log(data);
        var bounds = new google.maps.LatLngBounds();        
        for (var i = 0, len = data.sector.length; i < len; i++) {
            coords = data.sector[i].geometry.coordinates[0];
            siteNames = data.sector[i].properties.Name; // added for site names
            health = data.sector[i].properties.Health;
            calls = data.sector[i].properties.Calls;
            drops = data.sector[i].properties.Drops;
            blocks = data.sector[i].properties.Blocks;
            TCCFs = data.sector[i].properties.TCCFs;
            dcr = data.sector[i].properties.DCR;
            TCCFpct = data.sector[i].properties.TCCFpct;
            path = [];
        for ( var j = 0, len2 = coords.length; j < len2; j++ ){ // pull out each     set of coords and create a map object
            var pt = new google.maps.LatLng(coords[j][1], coords[j][0])
            bounds.extend(pt);
            path.push(pt);

        }

        if ( health == 1 ) 
        {
            var polygons = new google.maps.Polygon({
            path: path,
                strokeColor: "#000000",
                strokeOpacity: 0.8,
                strokeWeight: 1,
                fillColor: "#FF0000",
                fillOpacity: 0.35,
            map: map
            });
        } else {
            var polygons = new google.maps.Polygon({
            path: path,
                strokeColor: "#000000",
                strokeOpacity: 0.8,
                strokeWeight: 1,
                fillColor: "#000000",
                fillOpacity: 0.35,
            map: map
            });
        }   
        createClickablePoly(polygons, "Site: " + siteNames + "</br>" + "Total_calls " + calls + "</br>" + "Total_drops " + drops 
            + "</br>" + "Total_blocks " + blocks + "</br>" + "Total_TCCF " + TCCFs + "</br>" + "DCR " + dcr + "</br>" + "TCCFR " + TCCFpct);


        google.maps.event.addListener(polygons, 'mouseover', function() {
        var currentPolygon = this;
        currentPolygon.setOptions({ 
            fillOpacity: 0.45,
            fillColor: "#FF0000"
            })
        });

        if ( health == 1 ) 
        {
            google.maps.event.addListener(polygons, 'mouseout', function() {
            var currentPolygon = this;
            currentPolygon.setOptions({ 
                fillOpacity: 0.35,
                fillColor: "#FF0000"
                })
            });
        } else {
            google.maps.event.addListener(polygons, 'mouseout', function() {
            var currentPolygon = this;
            currentPolygon.setOptions({ 
                fillOpacity: 0.35,
                fillColor: "#000000"
                })
            });
        }   

        allPolygons.push(polygons);
        }
    }
4

1 回答 1

0

可能当调用 set_timeout 时,调用 $.ajax 的底层函数已经终止,因此变量“数据”被删除?$.ajax() 是一个异步函数...

例子:

function test() {
    var bla = 'test';
    $.ajax({
        // config
        success  : function( data ) {
            alert(bla); // should be 'undefined';
        }
    });
}

可能的解决方案:

  1. 要么使变量成为全局变量(始终有效),要么
  2. 如果在 success: 函数中声明了变量,则将其传递给 set_timeout 函数。如果没有,请通过它。

老实说,我不知道这个的第二次调用是什么变量

next = JSON.parse(data.next);

指。因为,正如您在帖子的评论中所写,我相信您在上面 2 行有语法错误。

于 2012-11-14T22:37:07.573 回答