Monday, February 6, 2012

Handling Drupal json HTML responses in jQuery

drupal_json($output); used to send server side responses in Drupal. Here to process html responses from an array in $.ajax success() we need to use eval() in order to handle encrypted responses. Following is the sample code.

<?php
$output['FirstTable'] = "<table><tr><td>1</td></tr></table>";
$output['SecTable'] = "<table><tr><td>2</td></tr></table>";
echo drupal_json($output); exit;
?>
In Script file

$.ajax({
"url": <url>,
"success": function(output) {
var content = eval("(" + output + ")"); // response is converted as object
$("selector").html(content.FirstTable);
$("selector").html(content.SecTable);
}
});

No comments:

Post a Comment