gpt4 book ai didi

javascript - jVectorMap 加载 ajax 数据时出现问题

转载 作者:行者123 更新时间:2023-12-01 05:44:05 25 4
gpt4 key购买 nike

我正在尝试在使用 jvectormap 插件创建的 map 内显示访问者的数据。

这让我发疯,我无法通过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}]}

现在加载 map 的函数:

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 魔法就开始了

关于javascript - jVectorMap 加载 ajax 数据时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28567465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com