我正在從PHP服務器回傳JSON到瀏覽器。我很熟悉XML,但是對於JSON來說是新的。有人可以告訴我如何從xmlhttpRequest中正確提取JSON,然後將它傳遞給數據或警報。在JS中通過xmlhttp請求傳遞JSON
我的JSON(從PHP服務器)
{"data": {
"message": "Open the Pod bay doors, Hal",
"type": "request",
"replies" => array(
"id": "12321"
"message": "I'm sorry Dave, I'm afraid I can't do that!"
)
}
}
我在JS請求返回的JSON但是我沒有抓住它或提取內幕信息的途徑......我的AJAX功能...
function ajax(site){
xmlhttp.open("GET","site",true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4) {
if (xmlhttp.status!=404) {
var resp =new Function("return "+xmlhttp.responseText)();
}
}
xmlhttp.send(null);
}
然後我調用在在window.onload
window.onload = runJSON()
function runJSON(){
var site = "http://localhost/sites/sandbox/json.php"
ajax(site);
... this is what i am unsure about... how do I access the data in the object
}
alert(data);
}