【问题标题】:Correct way to read 'echo json_encode( )' from JQuery从 JQuery 中读取“echo json_encode()”的正确方法
【发布时间】:2011-07-06 15:42:28
【问题描述】:

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

我的 PHP 代码:

$Stuff = 'Hello world';

$Success = true;
$Content = $Stuff;

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

我的 JS 代码:

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);}
});  

【问题讨论】:

    标签: php jquery ajax echo json


    【解决方案1】:

    你也应该设置 mime 类型,根据this questionapplication/json。然后 jQuery 就会明白答案是一个 json 元素。为此,您需要执行以下操作:

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

    在打印任何内容之前,在您的UpdateEditAStudent.php 中。

    【讨论】:

    • @Lumbenil。非常感谢你。与 Niklaas 建议的在 $.ajax dataType: "json" 中定义正确的数据类型相比,您的解决方案有什么优势?
    • UpdateEditAStudent.php 的正确性,因为现在 Apache(或任何网络服务器)告诉任何下载给定 URL 是 HTML 页面(mime 类型 text/html)的人,而不是真的mime 类型,即application/json
    【解决方案2】:

    您不必在 PHP 文件中添加标头,只需使用此Jquery parseJSON function

    保持此 PHP 代码原样:

    $Stuff = 'Hello world';
    
    $Success = true;
    $Content = $Stuff;
    
    $Response = array('Success' => $Success, 'Content' => $Content);
    echo json_encode($Response);
    

    对于 JS:

    $.ajax({  
        type: "GET",
        url: "../pgs/UpdateEditAStudent.php",
        data: "FirstName="+ $('#student_first_name').val(),
    
        success: function(data){
            // Here is the tip
            var data = $.parseJSON(data);
    
            alert(data.Content);
        }
    });
    

    【讨论】:

      【解决方案3】:

      您需要定义正确的dataType 或提供正确的标头,如 Lumbendil 所述。

      您可以手动将dataType 定义为json,因此您的代码如下所示:

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

      【讨论】:

        【解决方案4】:

        这是一个数组。你可能应该做 alert(data['Content']);.

        【讨论】:

        • Javascript arrays with key=>value?
        【解决方案5】:

        做这样的事情

        $Stuff = 'Hello world';
        
        $Success = true;
        $Content = $Stuff;
        
        $Response = array('Success' => $Success, 'Content' => $Content);
        echo json_encode($Response);
        

        【讨论】:

        • 他询问了来自 JQuery 的编码。你给出了一个来自 php.ini 的数组示例。他已经做到了。
        猜你喜欢
        • 2014-12-06
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多