javascript - 我如何查看 AJAX 传递的数据

标签 javascript php jquery mysql ajax

我希望能够查看 AJAX 传递的数据是否是函数 sendToServer 中的正确数据。

当用户提交他/她想要的数据时,提交函数将其发送到next.php。我想查看 next.php 正在接收什么,我该怎么做?它应该收到与此处相同的内容:

$("#result").html(JSON.stringify(arr));

这样我就可以将数据插入 MySQL 数据库。

next.php:

<?php

$data = json_decode(stripslashes($_POST['arr']));
foreach($data as $item){
   echo $item;
   // insert to db
}

?>

我目前的代码在代码片段中:

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
  <style type="text/css">
    <!-- #main {
      max-width: 800px;
      margin: 0 auto;
    }
    -->
  </style>
</head>

<body>
  <div id="main">
    <h1>Add or Remove text boxes with jQuery</h1>
    <div class="my-form">
      <!--            <form action="next.php" method="post">-->
      <button onclick="addAuthor()">Add Author</button>
      <br>
      <br>
      <div id="addAuth"></div>
      <br>
      <br>
      <button onclick="submit()">Submit</button>
      <!--            </form>-->
    </div>

    <div id="result"></div>
  </div>

  <script type="text/javascript">
    var authors = 0;

    function addAuthor() {
      authors++;
      var str = '<br/>' + '<div id="auth' + authors + '">' + '<input type="text" name="author" id="author' + authors + '" placeholder="Author Name:"/>' + '<br/>' + '<button onclick="addMore(\'auth' + authors + '\')" >Add Book</button>' + '</div>';
      $("#addAuth").append(str);
    }

    var count = 0;

    function addMore(id) {
      count++;
      var str =
        '<div id="bookDiv' + count + '">' + '<input class="' + id + '" type="text" name="book' + id + '" placeholder="Book Name"/>' + '<span onclick="removeDiv(\'bookDiv' + count + '\')">Remove</span>' + '</div>';
      $("#" + id).append(str);
    }

    function removeDiv(id) {
      $("#" + id).slideUp(function() {
        $("#" + id).remove();
      });
    }

    function submit() {
      var arr = [];
      for (i = 1; i <= authors; i++) {
        var obj = {};
        obj.name = $("#author" + i).val();
        obj.books = [];
        $(".auth" + i).each(function() {
          var data = $(this).val();
          obj.books.push(data);
        });
        arr.push(obj);
      }
      sendToServer(arr);
      $("#result").html(JSON.stringify(arr));
    }

    function sendToServer(data) {
      $.ajax({
        type: "POST",
        data: {
          arr: JSON.stringify(data)
        },
        url: "next.php",
        success: function() {

        }
      });
    }
  </script>
</body>

</html>

最佳答案

您的 js 正在发送一个 post 请求,因此您应该像收到一个普通的 html 表单 post 一样接收发送的数据。 尝试 var_dump($_POST); 查看您的数据在哪些索引名称下,然后您可以根据需要使用这些索引名称来操作数据。

关于javascript - 我如何查看 AJAX 传递的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34374422/

相关文章:

javascript - MEAN 堆栈和 bootstrap/bower 问题

php - MySql使用html日期输入选择带有日期时间的日期范围

Javascript - 求对称差 - For 循环错误

javascript - 如何从多个 SELECT 标签中获取值 (Jquery)

php - 获取分组字段的计数,但也需要计数为零的字段名称

jQuery:在尝试提交之前验证不起作用

javascript - 如何完全替换 HTML 元素上的数据对象

jquery - mvc中如何用jquery ajax获取数据?

javascript - jQuery - TableSorter 不工作

php - 推迟 MySQL 中的频繁更新