duan5362 2013-07-03 14:14
浏览 76
已采纳

jquery ajax没有从php解析json数据

I'm facing a strange problem for the last 10 hours and its really very annoying. The problem is with jquery printing json data from php. The php script is running fine, but when the ajax call returns in complete: event i'm not getting any valid otput.

here is the jquery code::

list_choice = "A";
content_choice = "Artists";             //globals to store default value

$(document).ready(function() {
$('.list-nav > a').click(function() {
    var ltext = $(this).text();
    list_choice = ltext;
    console.log(ltext+" <------>    ");        
    $.ajax({            
        url: 'retrieveFileFront.php',
        data: {type: content_choice, navtext: list_choice},
        type: 'POST',
        dataType: 'json',            
        complete: function(data) {                
            console.log(data['message']['Album_Name']);          
        }
    });
    return false;
});

});

i had to use complete: event as success: didn't worked at all. Atleast i'm getting some sort of output from the complete: event, although its giving undefined or [object][Object] which is totally ridiculous.

here is the retrieveFileFront.php:

<?php

require './retrieveFiles.php';

$type = $_POST['type'];
$nav_text = $_POST['navtext'];

$ret_files = new retrieveFiles($type, $nav_text);
$data = $ret_files->retFiles();
if ($data['success'] == FALSE) {
    $data = array('success' => FALSE, 'message' => 'Sorry an Error has occured');
    echo json_encode($data);

} else {
    echo json_encode($data);
}

?>

and here is the /retrieveFiles.php

<?php

class retrieveFiles {        
    public $content_type;
    public $list_nav;
    public $connection;
    public $result;
    public $result_obj;
    public $tags_array;
    public $query;
    public $row;

    public function __construct($type, $nav_text) {            
        $this->content_type = $type;
        $this->list_nav = $nav_text;     
    }

    public function retFiles() {

        @$this->connection = new mysqli('localhost', 'usr', 'pass', 'data');
        if(!$this->connection) {
            die("Sorry Database connection could not be made please try again later. Sorry for the inconvenience..");
        }

        if ($this->content_type == "Artists") {                

            $this->query = "SELECT album_name, album_art FROM album_dummy NATURAL JOIN album_images_dummy WHERE artist_name LIKE '$this->list_nav%'";               

            try {
                $this->result = $this->connection->query($this->query);
                $this->row = $this->result->fetch_row();

                if (isset($this->row[0]) && isset($this->row[1])) {
                    $this->tags_array = array("success" => true, "message" => array("Album_Name" => $this->row[0], "Album_Art" => $this->row[1]));

                    return $this->tags_array;
                }

        }   catch (Exception $e) {                
                echo 'Sorry an Error has occurred'.$e;
                return false;
            }
        }
   }

}

?>

I'm getting a 200 response in console in firebug, which indicates that its running okay.

<!DOCTYPE HTML>

{"success":true,"message":{"Album_Name":"Streetcleaner","Album_Art":"\/var\/www\/html\/MusicLibrary\/Musics\/1989 - Streetcleaner\/folder.jpg"}}

Now this is making me even more confused as i can see that the json is formatted properly. Please provide any sort of suggestion on how to solve this problem.

Thanks in advance..

  • 写回答

4条回答 默认 最新

  • dpsyssiv90846 2013-07-03 14:20
    关注

    JSON encoded data is usually not sent like

    data['message']['Album_Name']);
    

    But rather like:

    data.message.Album_Name;
    

    You're calling your results the wrong way. These are not associative arrays anymore but are now objects, as the name JSON (JavaScript Object Notation) suggests.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题