在php查询数据库返回json类型结果一文中提供了使用php访问数据库并返回json类型的数据,这里在js中请求该php地址,并将json结果转换为数组。
function showHint()
{
if (window.XMLHttpRequest)
{
// IE7+, Firefox, Chrome, Opera, Safari 浏览器执行的代码
xmlhttp=new XMLHttpRequest();
}
else
{
//IE6, IE5 浏览器执行的代码
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var jsonStr=xmlhttp.responseText;
var jsonObj = JSON.parse(jsonStr);
for(var i =0 ;i < jsonObj.length;i++){
markerArr.push(jsonObj[i]);
console.log("showHint:"+(markerArr.length));
console.log("showHint:"+(markerArr[i].icon.x));
}
}
}
xmlhttp.open("GET","./map.php",true);
xmlhttp.send();
}