简体   繁体   中英

Passing JSON from PHP to a JavaScript variable

I'm making an aplication with phonegap and I'm stuck trying to send JSON data from the PHP on the server to JavaScript on the device. I want to do something like:

var JSON = '{ "Table" : ' + "http://www.hel.net/LoadDB.php=?table=exhibitions" +  '}';

the php works fine and returns somethig like:

"[{"id":"1","name":"Art for everyone","image":null,"name2":"June 29, 2013: 11:00am.","description":"With reservation\r\nFree entrance","description2":null}]"

I want that result in a javascript variable to work later with:

var obj = eval ("(" + JSON + ")");
document.getElementById("rName").innerHTML=obj.Table[1].name;
document.getElementById("lname").innerHTML=obj.Table[1].name2; 

What I want to do is something like:

var JSON = '{ "Table" : ' + "http://www.hel.net/LoadDB.php=?table=exhibitions" +  '}';
var obj = eval ("(" + JSON + ")");
document.getElementById("rName").innerHTML=obj.Table[1].name;
document.getElementById("lname").innerHTML=obj.Table[1].name2; 

How can I make the first line work? is it possible to make the first line work? PS. I do not have much experience with JSON arrays.



Ok I tried ajax and works, I used:

console.log("before"); 

var jqxhr = $.ajax( "http://www.hel.com/LoadDB.php?table=exhibitions" )
            .done(function(data) { console.log(data); })
            .fail(function() { console.log("error"); })
            .always(function() { console.log("complete"); });

console.log("after");

more info in:

api.jquery.com

I think all you need is var obj = <?php echo $myjsonencodedvar; ?> var obj = <?php echo $myjsonencodedvar; ?>

or

var obj = <?php echo json_encode($myarray_or_object); ?>

Since I said "I think..." I decided to test it out. I found the following dump() function here on SO.

$arr=array(1,'biscuit'=>'gravy',3,4,5);
$json=json_encode($arr);
?>
<script>
  j=<?php echo $json; ?>;
 document.write(dump(j));

 function dump(obj) {
    var out = '';
    for (var i in obj) {
        out += i + ": " + obj[i] + "\n";
    }

    return out;
}
</script>

output:

0: 1 biscuit: gravy 1: 3 2: 4 3: 5

使用JSONP(无回调),并在客户端使用$ .getJSON(),它将从json字符串解析为js对象。

Try this:

PHP: (json.php)

<?php
    header("Content-Type: text/json");

    //the data format your question mentioned
    $data = array("Table"=>array(array("id"=>"1","name"=>"Art for everyone","image"=>null,"name2"=>"June 29, 2013","description"=>"With reservation\r\nFree entrance","description2"=>null)));

    echo json_encode($data);
?>

Front-end:

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
    $.get("json.php",function(json){
        alert(json.Table[0].name);
    });
</script>
</body>
</html> 

Hope this is helpful for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM