Php 如何使用ajax使用json输出正确的html
您好,我想将Php 如何使用ajax使用json输出正确的html,php,javascript,json,Php,Javascript,Json,您好,我想将[json['information']中的代码作为实际转换的html代码输出 目前,它似乎只是将整个字符串作为纯文本输出(未格式化的html,因此您可以看到所有标记等) 我还在学习json,所以我不确定应该如何处理接收到的内容,使其成为正确的html 提前谢谢 $('.engineering-services').live('click', function() { $.ajax({ url: 'index.php?route=information/inform
[json['information']
中的代码作为实际转换的html代码输出
目前,它似乎只是将整个字符串作为纯文本输出(未格式化的html,因此您可以看到所有标记等)
我还在学习json,所以我不确定应该如何处理接收到的内容,使其成为正确的html
提前谢谢
$('.engineering-services').live('click', function() {
$.ajax({
url: 'index.php?route=information/information/homepage_info',
type: 'post',
data: {info_for : 'engineering'},
dataType: 'json',
success: function(json){
$('#engineering-content').html(json['information']);
},
error: function(json) {
alert('fail');
}
});
});
编辑,这里是PHP
public function homepage_info() {
$this->load->model('catalog/information');
$json = array();
if (isset($this->request->post['info_for'])) {
if ($this->request->post['info_for'] == 'engineering') {
$information_info = $this->model_catalog_information->getInformation(10);
$json['information'] = $information_info['description'];
}
$this->response->setOutput(json_encode($json));
}
}
您的HTML字符串似乎将特殊字符编码为HTML实体,例如
看起来您正在为您的php控制器使用Open cart。为了使用正确的json头返回响应,Open cart的方式如下:
$this->load->library('json');
$this->response->setOutput(Json::encode($json));
希望这能有所帮助。您可能在编码像我编辑的这样的东西,以包含php,如果这是一个愚蠢的问题,很抱歉,但您所说的JSON部分是什么意思?就像在实际输出中一样?另外,我想您关于<等的说法是对的,但是有没有办法解决这个问题呢?是的,我在谈论JSON输出。但是试试这个:$this->response->setOutput(json_encode(html_entity_decode($json));
。看起来数据已经在模型/数据库中编码。实际上:$json['information']=html_entity_decode($information_info['description']);