dongren5293 2016-12-14 14:35
浏览 66

无法打印ajax成功函数数据

I am creating a wordpress plugin that consist a form with Ajax Functionality.

I am able to see the json array as below in console tab while debugging.

Note: Error Function in Ajax Call displays below result:

Object {readyState: 4, responseText: "[{"id":"1","school_name":"Apollo Beach Elementary …lo Beach","rating":"A","type":"Elementary"}]Array", status: 200, statusText: "OK"}

Here is the Ajax Code

function ajaxformschool() {
        jQuery.ajax({
            url: ajaxschoolajax.ajaxurl,
            data: {'action':'ajaxschool_process'},
            dataType: 'json',
            type: "POST",
            success: function(data) {
                alert(data[0]);         
        },
     error:function(exception){console.log(exception);}
        });

}

Html link where the function is called

echo '<a onclick="ajaxformschool();" style="cursor: pointer"><b>Search</b></a>';    

Ajax Action Function

    function ajaxschool_process() {
    global $wpdb;
    $data = $wpdb->get_results ("SELECT * FROM schools;");

    echo json_encode($data);
    die($data);
}
  • 写回答

3条回答 默认 最新

  • doure8758 2016-12-14 14:48
    关注

    Do not pass $data in die() function :

    $data = $wpdb->get_results ("SELECT * FROM schools;");
    echo json_encode($data);
    die();
    

    die() tries to print the value passed in parameter, then prints "Array", so jQuery can't parse the JSON data since it's not a valid JSON.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!