Return both JSON and HTML in AJAX call Return both JSON and HTML in AJAX call json json

Return both JSON and HTML in AJAX call


So presumably you have two variables: a HTML string ($html_string) and an PHP array of information which will be sent in JSON format ($array_of_info). Then:

$a = array(  'html' => $html_string,  'json' => $array_of_info);header('Content-Type: application/json');echo json_encode($a);

The output from this is slightly longer than that from simply concatenating the HTML and JSON strings, but the parsing on the client side should be easier. And probably faster. And, as pointed out in the comments, less error-prone.


Why don't you return the HTML string as part of the JSON? This way, your output is cleaner.