AJAX response "Json Data Undefined" in WordPress

I got a little problem with my AJAX response. This is my JavaScript code

$("#txt-cmt").keypress(function(e) {

    if(e.which == 13) {
        var comment = $(this).val();
        var message = {
            action: "show_comment",
            user_message: comment,
            dataType: 'json'

        $.post(ajaxUrl.url, message, function(data){

When I use console.log(data) it shows my real data like this:


Now my problem is when I access JSON data like this


It shows undefined and if I try to use JSON.parse(data); but I got error like this

 Uncaught SyntaxError: Unexpected token  in JSON at position 0 /wordpress/#:1
    at JSON.parse (<anonymous>)
    at Object.success (comment-mu.js?ver=4.7.5:26)
    at i (jquery.js?ver=1.12.4:2)
    at Object.fireWith [as resolveWith] (jquery.js?ver=1.12.4:2)
    at y (jquery.js?ver=1.12.4:4)
    at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)

And here is my PHP code with AJAX action hook

add_action("wp_ajax_show_comment", "mu_show_comment");

function mu_show_comment() {

    $message = isset($_POST['x']) ? $_POST['x'] : 'no message found';

    $info = [
        "content" => "apple"

    echo json_encode($info);

Read more here: AJAX response "Json Data Undefined" in WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *