Putting HTML in JSON

后端 未结 3 678
不知归路
不知归路 2020-12-18 21:30

As per title, is it considered a good practice to put HTML in JSON? The reason I need to do this is because I would like to have a custom dropdown where the list is coming f

3条回答
  •  既然无缘
    2020-12-18 22:16

    You wouldn't be the first to do it, and certainly not the last.

    To really answer the question, assuming you're following the protocol/standard and not breaking it (including quotes in the string without escaping them, for instance) you should be fine. json_encode does a great job at all this, but as @Kolink mentioned make sure you encode it to UTF8 first otherwise stray Unicode characters will occasionally break it resulting in empty output.

    Beyond that, it's programmer preference to use it. Some avoid it and keep the UI work on the page, others have the server generate the UI and let JavaScript just dump it--either way it's your call, and perfectly acceptable.

提交回复
热议问题