我需要一个PHP代码来返回Javascript块内的数组对象,该对象位于我的 home.html 页面内。 这是PHP代码:
<?php
$fields=array('Lat','Lon');
$nodedata = array_fill_keys($fields, '');
$nodedata[0]= array("Nome" => "Lat1" , "Cognome" => "Lon2");
$nodedata[1] = array("Nome" => "Athens" , "Cognome" => "Belgrade");
$nodedata[2] = array("Nome" => "Athens" , "Cognome" => "Belgrade");
echo json_encode($nodedata);
?>
这是 home.html 文件:
<html>
<head>
</head>
<body>
<div id="id01">
</div>
<script type='text/javascript'>
function prova2() {
var xmlhttp = new XMLHttpRequest();
var url = "prova.php";
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
//var app=myFunction(xmlhttp.response);
// document.getElementById("id01").innerHTML=app; Fino a qui stampa tutto
return myFunction(xmlhttp.response);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
var result = "";
for (var a in arr){
if (arr[a].Lat != undefined) {
result += "Nome : " + arr[a].Nome + " Cognome: " + arr[a].Cogmone + "<br> ";
}
}
// document.getElementById("id01").innerHTML=result; Fino a qui stampa tutto
}
}
function prova3() {
var app2=prova2();
document.getElementById("id01").innerHTML=app2; //Qui non mi stampa nulla
}
</script>
</body>
</html>
无论如何,指令
document.getElementById("id01").innerHTML=app2;
失败,浏览器页面中没有显示任何内容。 你能帮助我吗? 感谢。