returning HTML in my json array, what things do I have to escape for?
在我的json数组中返回HTML,我必须要逃避什么?
3 个解决方案
#1
HTML can be passed in JSON if standard JSON escaping rules are applied. Any json library (worth its weight in bytes) will do this for you.
如果应用了标准JSON转义规则,则可以使用JSON传递HTML。任何json库(以字节为单位的重量)都会为你做这件事。
In PHP:
json_encode('<body class="foo">');
Returns
"<body class=\"foo\">"
More info on http://www.json.org/
有关http://www.json.org/的更多信息
#2
I think the answer is, you don't need to. JSON encoding will handling everything for you.
我认为答案是,你不需要。 JSON编码将为您处理一切。
However, depending on your other needs, if you want to strip tags, or make < into <, then you can do so to the HTML string first, or do it at the client using Javascript.
但是,根据您的其他需要,如果要剥离标记,或者使
#3
You don't need to escape anything. The Json serializer will take care of it:
你不需要逃避任何事情。 Json序列化器将负责它:
return Json(new { html = "<html><body><div class=\"foo\">Hello</div></body></html>" });
#1
HTML can be passed in JSON if standard JSON escaping rules are applied. Any json library (worth its weight in bytes) will do this for you.
如果应用了标准JSON转义规则,则可以使用JSON传递HTML。任何json库(以字节为单位的重量)都会为你做这件事。
In PHP:
json_encode('<body class="foo">');
Returns
"<body class=\"foo\">"
More info on http://www.json.org/
有关http://www.json.org/的更多信息
#2
I think the answer is, you don't need to. JSON encoding will handling everything for you.
我认为答案是,你不需要。 JSON编码将为您处理一切。
However, depending on your other needs, if you want to strip tags, or make < into <, then you can do so to the HTML string first, or do it at the client using Javascript.
但是,根据您的其他需要,如果要剥离标记,或者使
#3
You don't need to escape anything. The Json serializer will take care of it:
你不需要逃避任何事情。 Json序列化器将负责它:
return Json(new { html = "<html><body><div class=\"foo\">Hello</div></body></html>" });