从PHP JSON对象值对象、PHP、JSON

2023-09-11 01:08:45 作者:放手的前提是拥有

我在PHP中使用JSON,现在我需要从JavaScript访问它。我如何通过一个JSON对象的JavaScript?

I am using JSON in PHP, and now I need to access it from JavaScript. How do I pass a JSON object to JavaScript?

<?php
    $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");
    $json = json_encode($array);
>

在这里My.js有:

where My.js has:

showAll(){
    alert("Show All Json Objects");
    // How do I get the JSON value here?
}

我该怎么办呢?

How can I do it?

推荐答案

假设你正在使用Ajax作为你的方法下载JSON,你会呼应的json_en code的结果:

Assuming that you're using Ajax as your method to download the JSON, you would echo the result of the json_encode:

<?php
    $array = array("a"=>"Caucho", "b"=>"Resin", "c"=>"Quercus");

    echo json_encode($array);
?>

然后您回电事件中,你会EVAL响应:

And then within your call back event, you'd eval the response:

var obj = eval('(' + req.ResponseText + ')');
for(var i in obj) {
    alert(i + ': ' + obj[i]);
}

假设你有一个 XMLHtt prequest 对象名称 REQ