Sending HTML Code Through JSON

前端 未结 7 1060
暗喜
暗喜 2020-11-30 07:07

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?

7条回答
  •  有刺的猬
    2020-11-30 07:56

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

    I'm the markup

    "}

提交回复
热议问题