从JQuery读取'echo json_encode()'的正确方法

问题描述:

我正在使用:echo json_encode($ Response);将关联数组发送回JQuery Ajax.每当我尝试读取每个ID密钥值时,我都会得到一个未定义的值.请帮我弄清楚我做错了什么...预先感谢

I am using: echo json_encode($Response); to send an Associative array back to JQuery Ajax. Whenever I try to read each ID key value I get an undefined value. Please help me figure out what I am doing so wrong... Thanks in advance

我的PHP代码:

$Stuff = 'Hello world';

$Success = true;
$Content = $Stuff;

$Response = array('Success' => $Success, 'Content' => $Content);
echo json_encode($Response);

# #

我的JS代码:

# #

My JS code:

var sFirstName     = $('#student_first_name').attr('value');  

$.ajax({  
    type: "GET",  
    url: "../pgs/UpdateEditAStudent.php", 
    data: "FirstName="+ sFirstName ,  

    //The below code will give me: {"Success":true,"Content":"Hello world"}
    success: function(data){$("#Ajax_response").html(data);}

    //The popup window will show me "Undefined"
    //and: {"Success":true,"Content":"Hello world"}
    success: function(data){$("#Ajax_response").html(data); alert(data.Content);}
});  

您还应根据

You should set the mime type aswell, wich, according to this question is application/json. Then jQuery will understand the answer is a json element. To do it, you'd do the following:

header('Content-Type: application/json');

UpdateEditAStudent.php中打印任何内容.