0

I built a message inbox, when you click on the message title an AJAX call is being made. I want to know how can i reply to the call using JSON (server-side).

Also how can i use the JSON returned to me to extract the data.

$.ajax({
    type: 'POST',
    url: 'ajax_handler.php',
    data: ({
        ajaxHook: 'getMessageReplies',
        messageID: $(this).attr('class')
    }),
    success: function ( messageLayout ){
    }
});

thanks in advance! :)

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

1 Answers1

1

in your ajax_handler.php you can do something like

<? php

var $ajaxHook = $_POST["ajaxHook"];
var $messageID= $_POST["messageID"];

//perform some processing

$arr = array("title" => "john", "yourHtml" => "<p>hello</p>");
echo json_encode($arr);

?>

set the dataType:'json' so that the json is parsed

$.ajax({
    type: 'POST',
    url: 'ajax_handler.php',
    dataType:'json',
    data: ({
        ajaxHook: 'getMessageReplies',
        messageID: $(this).attr('class')
    }),
    success: function ( data ){    

      alert(data['title']);
      alert(data['yourHtml']);
    //process the result sent by the server
    }
});
Rafay
  • 30,950
  • 5
  • 68
  • 101