dqhr76378
2013-06-27 01:43
浏览 87

将JSON从PHP传递给JavaScript变量

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
Free 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

图片转代码服务由CSDN问答提供 功能建议

我正在使用phonegap做一个应用程序,我一直试图从服务器上的PHP发送JSON数据 到设备上的JavaScript。 我想做类似的事情:

  var JSON ='{“Table”:'+“http://www.hel.net/LoadDB.php=?table= 展览“+'}'; 
   
 
 

php工作正常并返回类似:

 ”[{  “id”:“1”,“name”:“Art for everyone”,“image”:null,“name2”:“2013年6月29日:上午11:00。”,“description”:“预约
 \”  nFree入口“,”description2“:null}]”
   
 
 

我希望javascript变量中的结果能够在以后使用:

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

我想要做的是:

  var JSON =  '{“表”:'+“http://www.hel.net/LoadDB.php=?table=exhibitions”+'}'; 
var obj = eval(“(”+ JSON +“)”);  
document.getElementById( “RNAME”)的innerHTML = obj.Table [1]。名称; 
document.getElementById( “L-NAME”)的innerHTML = obj.Table [1] .name2;  
   
 
 

如何使第一行有效? 是否可以使第一行工作? PS。 我对JSON数组没有多少经验。



好的我试过ajax并且工作,我用过:
  console  .LOG( “前”);  
 
var jqxhr = $ .ajax(“http://www.hel.com/LoadDB.php?table=exhibitions”)
 .done(function(data){console.log(data);})\  n .fail(function(){console.log(“error”);})
 .always(function(){console.log(“complete”);}); 
 
console.log(“after”  ); 
   
 
 

更多信息:

api.jquery.com

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongyan1936 2013-06-27 02:34
    已采纳

    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
    Free 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.

    已采纳该答案
    打赏 评论
  • dongyoulou4829 2013-06-27 01:47

    I think all you need is 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] + "
    ";
        }
    
        return out;
    }
    </script>
    

    output:

    0: 1 biscuit: gravy 1: 3 2: 4 3: 5
    
    打赏 评论
  • dpdt79577 2013-06-27 01:49

    using JSONP (no callbacks), and on the client side use $.getJSON() it will parse it from json string to js object.

    打赏 评论

相关推荐 更多相似问题