通过 JSON 发送 HTML 代码 [英] Sending HTML Code Through JSON
问题描述
我有一个生成 HTML 内容的 php 脚本.有没有办法通过 JSON 将 HTML 内容从 php 脚本发送回我的网页?
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?
推荐答案
是的,你可以使用 json_encode
获取您的 HTML 字符串并根据需要对其进行转义以使其有效 JSON (它也会做一些不必要的事情,遗憾的是,除非你使用标志来阻止它).例如,如果您的原始字符串是:
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);
产生:
You'll notice it has an unnecessary backslash before the /
near the end. You can use the JSON_UNESCAPED_SLASHES
flag to prevent the unnecessary backslashes. json_encode(theString, JSON_UNESCAPED_SLASHES);
produces:
"<p class=\"special\">content</p>"
这篇关于通过 JSON 发送 HTML 代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!