Wil*_*aan 9 javascript php ajax
我有这个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();
Run Code Online (Sandbox Code Playgroud)
我有一个PHP数组
$cars=array("Saab","Volvo","BMW","Toyota");
我如何将数组$ cars发送到我的javascript?
Der*_*會功夫 16
echo json_encode($cars);
Run Code Online (Sandbox Code Playgroud)
原生:
var foo = JSON.parse(xmlhttp.responseText);
Run Code Online (Sandbox Code Playgroud)
使用jQuery:
var foo = $.parseJSON(xmlhttp.responseText);
//or
$.getJSON("url", function(data){
//data is your array
});
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
如果你想使用jQuery,请将其放入<head>:
<script type="text/javascript" src="link/to/the/file/jquery.js"></script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
19860 次 |
| 最近记录: |