加载ajax数据时出现jVectorMap问题

jVectorMap Issue while loading ajax data

本文关键字:jVectorMap 问题 ajax 数据 加载      更新时间:2023-12-11

我试图在使用jvectormap插件创建的地图中显示访问者的数据。

这让我抓狂,我无法通过ajax加载数据,如果我手动放置数据,它就可以工作了。

到目前为止,我有这个:

map.php

$datos = array();
$link->set_charset("utf8");
$sql = $link->query("SELECT SUM(ID) as visitors, state FROM visitors WHERE state != '' GROUP BY state");
while($row = $sql->fetch_row()){
    $ss = $link->query("SELECT * FROM states WHERE state = '".$row[1]."'");
    $rr = $ss->fetch_row();
    $datos[] = array("ccode" => $rr[2], "visits" => $row[0]);
}
$data = array("countries" => $datos);
echo json_encode($data,JSON_NUMERIC_CHECK);

这将返回以下数据:

{"countries":[{"ccode":"VE-A","visits":81},{"ccode":"VE-L","visits":24}]}

现在加载地图的功能:

function cargaMapa(){
        //jvectormap data
    $.post("ajax/map.php",{},function(mapa){
        var dataC = eval(mapa);
        //var dataC = {"countries":[{"ccode":"VE-A","visits":81},{"ccode":"VE-L","visits":24}]};
        var countryData = []; 
        //for each country, set the code and value
        $.each(dataC.countries, function() {
            countryData[this.ccode] = this.visits;
            console.log("Estado: "+this.ccode+" Visitas: "+this.visits);
        });
        //World map by jvectormap
        $('#world-map').vectorMap({
            map: 've_mill_en',
            backgroundColor: "#fff",
            regionStyle: {
                initial: {
                    fill: '#e4e4e4',
                    "fill-opacity": 1,
                    stroke: 'none',
                    "stroke-width": 0,
                    "stroke-opacity": 1
                }
            },
            series: {
                regions: [{
                        values: countryData,
                        scale: ["#3c8dbc", "#2D79A6"], //['#3E5E6B', '#A6BAC2'],
                        normalizeFunction: 'polynomial'
                    }]
            },
            onRegionLabelShow: function(e, el, code) {
                //search through dataC to find the selected country by it's code
                var country = $.grep(dataC.countries, function(obj, index) {
                    return obj.ccode == code;
                })[0]; //snag the first one
                //only if selected country was found in dataC
                if (country != undefined) { 
                    el.html(el.html() + ': ' + country.ccode + country.visits + ' visitas');
                }
            }
        });
    });
}

正如你在函数中看到的,我有var dataC,如果我在那里加载来自map.php的数组,它会给我Uncaught SyntaxError: Unexpected token :,但如果将map.php的结果复制并粘贴到var dataC中,它会非常好。

我该如何解决这个问题?

我感谢的任何帮助

感谢

我想明白了,只是把$.post改成了$.getJSON,魔术开始了