简体   繁体   English

将JSON从PHP传递到JavaScript变量

[英]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. 我正在使用phonegap进行复制,并且尝试将JSON数据从服务器上的PHP发送到设备上的JavaScript时遇到了麻烦。 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: PHP工作正常,并返回somethig像:

"[{"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: 我希望该结果可在javascript变量中稍后使用:

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. PS。 I do not have much experience with JSON arrays. 我对JSON数组没有太多经验。



Ok I tried ajax and works, I used: 好的,我尝试了ajax并工作了,我使用了:

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 api.jquery.com

I think all you need is var obj = <?php echo $myjsonencodedvar; ?> 我认为您只需要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. 我在SO上找到了以下dump()函数。

$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:(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. 希望这对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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