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条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog