php - json_encode 不显示 HTML

标签 php jquery html ajax

我正在从数据库中获取一些数据,其中一些数据有 HTML诸如“<p>Hello</ p>”之类的标签。但是,当单步执行数据返回时,它会在“&lt;p&gt;Hello&lt;/p&gt; ”中显示 json_encode 。我应该怎么做才能返回数据库中保存的相同数据?

PHP:

$retorna = array(
    'conteudo_programatico' => $conteudo_programatico,
    'investimento' => $investimento,
    'coordenacao' => $coordenacao,
    'importante' => $importante
);

echo json_encode($retorna);

AJAX:

$(function(){
    $("button").click(function(){
        var id = $(this).attr("id");
        $.ajax({  
            type: "POST",
            data: {id: id},
            url: "paginas/ajax/form.php",
            dataType: "json",
            success: function(data){
                $("#cursos").slideDown();
                $(".call-conteudo").text(data['conteudo_programatico']);
                $(".call-investimento").text(data['investimento']);
                $(".call-coordenacao").text(data['coordenacao']);
                $(".call-importante").text(data['importante']);
            },
        });
    });
});

最佳答案

问题是你使用了 jQuery 的 .text() :

$(".call-conteudo").text(data['conteudo_programatico']);
// etc.

根据manual :

We need to be aware that this method escapes the string provided as necessary so that it will render correctly in HTML. To do so, it calls the DOM method .createTextNode(), does not interpret the string as HTML

...

The code $( "div.demo-container" ).text( "<p>This is a test.</p>" ); will produce the following DOM output:

 1 <div class="demo-container">
 2   &lt;p&gt;This is a test.&lt;/p&gt;
 3 </div>

您需要 html() :

$(".call-conteudo").html(data['conteudo_programatico']);
// etc.

关于php - json_encode 不显示 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683245/

相关文章:

javascript - 在thickbox内提交表单

jquery - 如何使用图像映射来显示/隐藏特定的 div?

php - PHP 中的 ODBC 准备语句

php - CodeIgniter 的 Nginx 重写规则

javascript - jQuery TWBS 分页实现

javascript - 如何使用 jQuery 实现从一张图片到另一张图片的过渡效果?

javascript - 在 url 中使用 # 打开 html div 元素

php - 在 AJAX 后置过滤器函数中使用多个 'relation' 参数

php - 在多个 mysql 表之间保持更新的 mysql 数据

jQuery 如何设置第一个选项卡处于事件状态?