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?
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?
Yes, you can use json_encode
to take your HTML string and escape it as necessary to be valid JSON (it'll also do things that are unnecessary, sadly, unless you use flags to prevent it). For instance, if your original string is:
<p class="special">content</p>
.json_encode
将产生以下结果:
"<p class=\"special\">content<\/p>"
你会注意到在接近尾声的/
之前有一个不必要的反斜杠.您可以使用JSON_UNESCAPED_SLASHES
标志来防止不必要的反斜杠.json_encode(theString, JSON_UNESCAPED_SLASHES);
生产:
"<p class=\"special\">content</p>"