weixin_33705053 2018-06-21 17:38 采纳率: 0%
浏览 49

Ajax不显示PHP数据

I'm in a little trouble here. I'm trying to use ajax to get data from PHP server, that it gets from Mysql database; and then display into a specific html tag place. But, for some reason, nothing is showed off to html. I tested the PHP page and it works correctly. The point is, when ajax should get the data and display, it seems that there's nothing at database.

This is my html target :

<div class="container">

        <table>
                <thead>
                    <tr>
                        <th>Título</th>
                        <th>Curiosidade</th>

                    </tr>
                </thead>

                <tbody>

                </tbody>
            </table>
</div>  

This is my Ajax Script:

   function readData() {

          $.ajax({                                      
            type: "POST",
            dataType: "html",
            url: 'http://localhost/Gravidapp/php/read.php',                                           
            success: function(data){

              $('tbody').html(data);

            },
            error: function(xhr,desc,err){
                ajax.error(xhr);
                ajax.error(desc, err);
            } 
              });
          }; 

This is my PHP file:

   <?php

        require("bdconn.php");
        $pdo = new db();

        $pdo->mysql->beginTransaction();

        $rs = $pdo->mysql->query("select * from timeline");
        $rs->execute();

        while($row = $rs->fetch()){
        ?>
        <tr>
        <td><?php echo $row['titulocuriosidade']?></td>
        <td><?php echo $row['curiosidade']?></td>
        </tr>
        <?php
                  }

   ?>

Any suggests?

Thanks in advance

  • 写回答

2条回答 默认 最新

  • weixin_33727510 2018-06-21 18:14
    关注

    If the php response doesn't have the data type on headers ajax response could send an error.

    Try setting dataType="text html" on your ajax request this will try to convert the response from text to html

    also try to print errors on console to show whats is going wrong.

    error: function(xhr,desc,err){
        console.log(desc);
    }
    

    see dataType on: JQuery ajax

    评论

报告相同问题?