简体   繁体   English

JavaScript:解码PHP json_encode响应

[英]JavaScript: Decode a PHP json_encode response

For example, I am json_encoding my array and outputting it: 例如,我对数组进行json_encoding并输出:

$key = $this->key;
                    $sql = "SELECT * FROM Requests";                   
                    $result = $this->db->query($sql);
                    $temp = array();
                    foreach($result as $r)
                    {
                        if($r['key'] == $key){
                        array_push($temp, array(
                            'song' => $r['song'],
                            'artist' => $r['artist'],
                            'by' => $r['by']
                        ));
                        }
                    }
                    $this->encoded = json_encode($temp);
                    echo $this->encoded;

Then, I am sending a GET request to it: 然后,我向它发送一个GET请求:

$.get('http://www.example.com/file.php')
                .done(function(data){
                    alert(data['artist']);
                });

However, this alerts: 但是,此警报:

undefined 未定义

Can anyone possibly help me? 有人可以帮我吗? I've searched a lot and tried a lot but nothing seems to be working (like, JSON.Parse ). 我已经搜索了很多并尝试了很多,但是似乎没有任何效果(例如JSON.Parse )。

Edit: I tried adding the header('Content-Type: application/json'); 编辑:我试图添加header('Content-Type: application/json'); but it leaves me with this error: 但这给我留下了这个错误:

TypeError: data is null TypeError:数据为空


Thanks in advance! 提前致谢!

 $.get('http://www.syncsapi.x10host.com/API/Public/', { start: 'example' }) $.get('http://www.syncsapi.x10host.com/API/Public/', { display: '5' }) .done(function(data) { console.log(data[0]); }); }); 
 <meta content="text/html;charset=utf-8" http-equiv="Content-Type"> <meta content="utf-8" http-equiv="encoding"> <script src="//code.jquery.com/jquery-1.12.0.min.js"></script> <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 

So the problem with your script is that you are basically emitting something like this: 因此,脚本的问题在于,您基本上会发出以下内容:

[
  {
    "song"   : "...",
    "artist" : "..."
  }
]

But your javascript code is expecting something like this: 但是您的JavaScript代码期望这样的事情:

{
  "song"   : "...",
  "artist" : "..."
}

If your intention was to only ever send back one song for that PHP service, you should modify your PHP service to remove the top-level array. 如果您打算只为该PHP服务发回一首歌曲,则应修改PHP服务以删除顶级数组。

However, if you intend to send back more than 1 song, then the data you returned makes sense, but your javascript does not. 但是,如果您打算发回多首歌曲,则返回的数据很有意义,而JavaScript却没有。 To fix your javascript, you could simply change: 要修复您的JavaScript,您可以更改以下内容:

alert(data['artist']);

into: 成:

alert(data[0].artist);

Note that .artist and ['artist'] is the same, the real difference in my sample is that I added [0] which grabs the first item in the top-level array. 请注意, .artist['artist']是相同的,我样本中的真正区别是我添加了[0] ,它抓住了顶层数组中的第一项。

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

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