我无法执行ajax请求并获得json响应

时间:2019-08-03 15:15:53

标签: jquery json ajax

当我向我的php文件发出ajax请求时,我似乎无法收到json响应;

我尝试发送ajax请求并接收json响应。

我的Javascript:

$(".edit_data").click(function(){

            var id= $(this).attr("id");
            alert(id);
            $.ajax({
                url: "edit.php",
                type: "POST",
                data: {edit_id: id},
                dataType: "jsonp",
                contentType: "application/json; charset=utf-8",
                success: function(data){
                    var title=data.title;
                    var body=data.body;
                    $("#mtitle").val(data.title);
                    $("#msummernote").val(data.body);

                }

            });
            alert('t'.title);
            alert('b'.body);
            alert('hey');
            $('#myModal').modal('show'); 
        });

标题和正文的警报​​表明它们未定义 和我的edit.php:

<?php

   include 'db_connect.php';
   $id= $_POST['edit_id'];

   $stmt= "SELECT * FROM blog WHERE id=".$id;

   $result= $conn->query($stmt);

   $rows = $result->fetch_assoc();
   echo json_encode($rows);
?>

在我的html中,我有一个像这样的表:

    <table class="table-striped" style="width:80%">

                <tr>
                    <th>Title</th>
                    <th>Author</th>
                </tr>


                    <?php
                        while($rows = $result->fetch_assoc())
                        {
                    ?>
                <tr>

                    <td><?php echo $rows['title']?></td>
                    <td><?php echo $rows['uname']?></td>
                    <td><button name="edit" id="<?php echo $rows['id']; ?>" type="button" class=" edit_data btn btn-warning" >Edit  </button></td>
                    <td><button name="delete" id="<?php echo $rows["id"]; ?>" class="btn btn-danger">Delete</button></td>

                </tr>

                    <?php
                        }
                    ?>


            </table>


and the bootstrap modal i am trying to populate:


           

    <!-- Modal Header -->
    <div class="modal-header">
      <h4 class="modal-title">Edit Post</h4>
      <button type="button" class="close" data-dismiss="modal">&times;</button>
    </div>

    <!-- Modal body -->
    <div class="modal-body">
    <form method="post" id="insert_form">
        <label> Title: </label>
        <input name="mtitle" class="form-control" type="text" required placeholder="Title"/>
        <br><br>
        <label> Header Image: </label>
        <input class="form-control" type="file" name="file" id="file"><br><br>
        <label> Body: </label>
        <textarea name="content" id="msummernote"></textarea>

        <button class="btn btn-primary" name="submit"> Submit </button>

    </form>
    </div>

    <!-- Modal footer -->
    <div class="modal-footer">
      <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
    </div>

  </div>
</div>


I am trying to populate the field with id mtitle with the title i receive from  the json and the field with id msummernote with the body i recieve from the json

0 个答案:

没有答案