Sending html to Javascript from PHP via the JSON object


I'm sending some data from php through ajax to jquery.

If $content="ABC"; everything is OK. I get alert with ABC.

If $content="<div>ABC</div>"; then Houston has a problem. Nothing happens at all.

Here is PHP code

$json = json_encode(array("content" => $content));
echo $json;

And this is Jquery

 success: function(responseimage){
 result = jQuery.parseJSON(responseimage);

Any ideas ?

UPDATE! I've removed jQuery.parseJSON so that line has only this code

result = responseimage;

And now I get the result in alert. The result is the following


So we can see that JSON is not created well. I;ve tried utf8_encode and trim , but they do nothing to the result. result is strange.

PHP (and JSON) code seems fine, provided that the variable $content actually has any content. Else the PHP script will fail and there's your problem.

Did you define the dataType as "json" in the AJAX request?

    url: 'json.php',
    dataType: 'json',