Skip to content
Advertisement

Sending HTML Code Through JSON

I’ve got a php script which generates HTML content. Is there a way to send back that HTML content through JSON to my webpage from the php script?

Advertisement

Answer

Yes, you can use json_encode to take your HTML string and escape it as necessary.

Note that in JSON, the top level item must be an array or object (that’s not true anymore), it cannot just be a string. So you’ll want to create an object and make the HTML string a property of the object (probably the only one), so the resulting JSON looks something like:

{"html": "<p>I'm the markup</p>"}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement