【发布时间】:2012-01-03 08:40:13
【问题描述】:
我将 JSON 从 php 服务器回显到浏览器。我非常精通 XML,但对 JSON 很陌生。有人可以告诉我如何正确地从 xmlhttpRequest 中提取 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);
}
【问题讨论】:
标签: php javascript ajax json parameter-passing