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>"

Json相关问答推荐

PostgreSQL 12.17从JSON数组提取元素

Vega图表计数聚合如果数据值为空数组则不显示任何内容,如何解决此问题?

在MongoDB中检索某个月份和年份的JSON文档

无法访问id的第三级json

XSLT 3.0 Json-to-xml,json 包含 html struct

Jolt 不打印任何东西

我无法将来自 API 的数据显示到 FlatList 中,但我在 console.log 中看到了它.问题是什么?

使用 Jolt 使用键中的值创建带有硬编码键和值的 JSON 数组

无法向 Json 数组添加新元素

在 JSON 字符串中react i18n 换行符

如何将 LinkedHashMap 转换为自定义 java 对象?

在 Http Header 中使用 Json 字符串

如何在java中比较来自JsonObject的空值

JSON日期到Java日期?

Python 到 JSON 序列化在十进制上失败

处理 HTTP 请求正文中的可选 JSON 字段

如何使用 Gson 将 JSONArray 转换为 List?

使用 axios 在 POST multipart/form-data 请求中发送文件和 json

类型是接口或抽象类,不能实例化

JSON.stringify 向我的 Json 对象添加额外的 \ 和 "" 的问题