douchuifk90315 2017-06-23 06:40
浏览 278
已采纳

使用.html()不显示JSON数据

Before posting this question, I did search here and got different answers and I think it doesn't fit to my needs. I have a button when clicked, the following js script will run

$("#update-details-btn").on('click', function() {
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: "confirmation="+get_data,
            url: '../for_update_details',
            success: function(data)
            {
                console.log(data);
                $('div#update_details_body').html(data.results);

and this is the container

<div id="update_details_body" class="modal-body"></div>

the data comes from a php function

$data['results'] = NULL;
 $results = "<div class='form-group'>";
 $results .= "<label for='document-type' class='col-sm-4 control-label'>Category</label>";
 $results .= "</div>";
 $data['results'] = $results;
 echo json_encode($data);

As you can see from the js, I did a console.log which prints exactly what the data.results contain but when I use .html() to put inside the container, nothing happens. What could be the culprit here? I am doing this coding to other functions but only this section is not working

  • 写回答

4条回答 默认 最新

  • dtlrp119999 2017-06-23 09:31
    关注

    Code :

    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
        $("#update-details-btn").on('click', function() {
            alert("inside");
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'php_file.php',
                success: function(data)
                {
                    console.log(data);
                    $('div#update_details_body').html(data.results);
                }
            });
        });
    });
    </script>
    </head>
    <style>
    .modal-body
    {
        margin: 0px;
        padding: 0px;
        width: 100px;
        height: 100px;
    }
    </style>
    <body>
    <div id="update_details_body" class="modal-body"></div>
    <input type="button" id="update-details-btn" name="button" value="button">
    </body>
    </html>
    

    php code not chnage

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误