duanji2002 2015-04-16 18:35
浏览 64
已采纳

json_encode不显示HTML

I am bringing some data from the database, and some of them have HTML tags such as "<p>Hello</ p>". However, when step data through to return it shows json_encode in "&lt;p&gt;Hello&lt;/p&gt;". What should I do to return the same data that was saved in the database?

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']);
            },
        });
    });
});
  • 写回答

1条回答 默认 最新

  • dpjr86761 2015-04-16 18:40
    关注

    The problem is your use of jQuery's .text():

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

    According to the 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>
    

    You need html():

    $(".call-conteudo").html(data['conteudo_programatico']);
    // etc.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab