php 通过 JSON 发送 HTML 代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5064561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending HTML Code Through JSON
提问by Hirvesh
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?
我有一个生成 HTML 内容的 php 脚本。有没有办法通过 JSON 将 HTML 内容从 php 脚本发送回我的网页?
采纳答案by T.J. Crowder
Yes, you can use json_encode
to take your HTML string and escape it as necessary.
是的,您可以使用json_encode
来获取 HTML 字符串并根据需要对其进行转义。
Note that in JSON, the top level item mustbe 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:
请注意,在JSON 中,顶级项目必须是一个数组或对象(这不再是真的),它不能只是一个字符串。因此,您需要创建一个对象并使 HTML 字符串成为该对象的一个属性(可能是唯一的一个),因此生成的 JSON 类似于:
{"html": "<p>I'm the markup</p>"}
回答by Lead Developer
Do Like this
这样做
1st put all your HTML content to array, then do json_encode
1st 将所有 HTML 内容放入数组,然后执行 json_encode
$html_content="<p>hello this is sample text";
$json_array=array(
'content'=>50,
'html_content'=>$html_content
);
echo json_encode($json_array);
回答by blumanski
All string data must be UTF-8 encoded.
所有字符串数据都必须采用 UTF-8 编码。
$out = array(
'render' => utf8_encode($renderOutput),
'text' => utf8_encode($textOutput)
);
$out = json_encode($out);
die($out);
回答by orionrush
Just to expand on @T.J. Crowder's answer.
只是为了扩展@TJ Crowder 的回答。
json_encode
does well with simple html strings, in my experience however json_encode
often becomes confused by, (or it becomes quite difficult to properly escape) longer complex nested html mixed with php. Two options to consider if you are in this position are: encoding/decoding the markup first with something like [base64_encode][1]
/ decode(quite a bit of a performance hit), or (and perhaps preferably) be more selective in what you are passing via json, and generate the necessary markup on the client side instead.
json_encode
处理简单的 html 字符串效果很好,但根据我的经验,json_encode
经常被(或者很难正确转义)与 php 混合的更长的复杂嵌套 html 混淆。如果您处于此位置,要考虑的两个选项是:首先使用[base64_encode][1]
/ decode 之类的东西对标记进行编码/解码(对性能影响很大),或者(也许最好)对通过 json 传递的内容更具选择性,并在客户端生成必要的标记。
回答by Jan Zyka
You can send it as a String, why not. But you are probably missusing JSON here a bit since as far as I understand the point is to send just the data needed and wrap them into HTML on the client.
您可以将其作为字符串发送,为什么不呢。但是您可能在这里有点误用 JSON,因为据我所知,重点是仅发送所需的数据并将它们包装到客户端上的 HTML 中。
回答by Mārti?? Briedis
In PHP:
在 PHP 中:
$data = "<html>....";
exit(json_encode($data));
Then you should use AJAX to retrieve the data and do what you want with it. I suggest using JQuery: http://api.jquery.com/jQuery.getJSON/
然后你应该使用 AJAX 来检索数据并用它做你想做的事。我建议使用 JQuery:http: //api.jquery.com/jQuery.getJSON/