[英]Jquery ajax parse html from json response
I need select a div by id from html in json response. 我需要从json响应中的html通过id选择一个div。
In server side: 在服务器端:
ob_start();
extract($this->validate($this->data));
require("classes/View/". $this->view . ".phtml");
$content = ob_get_contents();
ob_end_clean();
header('Content-type: application/json');
echo json_encode(array(
'messages' => $this->data["messages"],
'content' => $content
));
and in client side: 并在客户端:
$.ajax({
type: "POST",
url: o.url ,
dataType: "JSON",
data: o.ajax_data,
success: function(response) {
$("#mydiv").html($(response.content).find("#mydiv"));
}
});
Before, when I used HTML as dataType and I returned plain HTML as response, everything have been functional. 以前,当我将HTML用作dataType并返回纯HTML作为响应时,所有功能都已正常运行。 I can't figure out the right solution even after hours of research.
经过数小时的研究,我仍然找不到正确的解决方案。 Can anybody help me please?
有人可以帮我吗?
===UPDATE 1=== ===更新1 ===
console.log(response.content) => {"messages":[],"content":"complete escaped html site here"} console.log(response.content)=> {“消息”:[],“内容”:“在此处完成转义的html网站”}
You are placing #mydiv
(placeholder element in response) into DOM placeholder #mydiv
. 您正在将
#mydiv
(响应中的占位符元素)放入DOM占位符#mydiv
。 What you are probably want to do is place content of #mydiv
from response into placeholder. 您可能要做的是将
#mydiv
内容从响应放入占位符。
Assuming your response.content
is properly formed html code this should work: 假设您的
response.content
是格式正确的html代码,那么它应该可以工作:
var content = $(response.content).find("#mydiv").html(); // get html of #mydiv in response
$("#mydiv").html(content);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.