我有这个ajax代码
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById('addIO').innerHTML+=xmlhttp.responseText;
}
}
xmlhttp.open("GET","http://localhost/Mar7ba/Ontology/getRelatedConceptsAndRelations/3/TRUE",true);
xmlhttp.send();
我有一个 php 数组
$cars=array("Saab","Volvo","BMW","Toyota");
我如何将数组 $cars 发送到我的 javascript ?
PHP
echo json_encode($cars);
JavaScript
Native:
var foo = JSON.parse(xmlhttp.responseText);
使用 jQuery:
var foo = $.parseJSON(xmlhttp.responseText);
//or
$.getJSON("url", function(data){
//data is your array
});
UPDATE
if(xmlhttp.readyState==4 && xmlhttp.status==200){
//document.getElementById('addIO').innerHTML+=xmlhttp.responseText;
var cars = JSON.parse(xmlhttp.responseText); //cars will now be the array.
//Do whatever you want here.
$("#addIO").html(cars.join(", ")); //Join array with ", " then put it in addIO
}
如果你想使用 jQuery,请将其放入<head>
:
<script type="text/javascript" src="link/to/the/file/jquery.js"></script>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)