dongtang3155
2019-05-12 06:17
浏览 225

var_dump() - $ _POST变量在通过jQuery的$ .post()方法发布字符串时显示空数组

Let's put examples to demonstrate my problem:

index.html:

<!doctype html>

<html>

  <head>

    <title>Testing XHR</title>

    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>

  </head>

  <body>

    <script>

      $(function()
      {
        const test = "This is a test text.";
        $.post("test.php", {data: test}, function()
        {
          console.log(test);
        });
      });

    </script>

  </body>

</html>

test.php:

<?php
var_dump($_POST);

So, when I load index.html, it completes $.post() request successfully. I have checked it via Chrome's "Inspect" option, under "Network" tab. But when I load test.php, it shows empty string. I have tested it to my local server and a live server, results are the same. What am I doing wrong?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douju1997 2019-05-12 06:26
    已采纳

    console log the response you get from test.php.

    $.post("test.php", {data: test}, function(response)
    {
        console.log(response);
    });
    

    loading the test.php won't show you the post data.

    已采纳该答案
    打赏 评论
  • dougaxing8673 2019-05-12 06:29

    say you sent data at 9 am by running html file. It sends data to php. but as php isn't returning response, you don't get that. now you ar running php file at say 9.01am. how will you get data now?

    You need to print out response.

    try the following.

    $(function()
          {
            const test = "This is a test text.";
            $.post("test.php", {data: test}, function(response)
            {
              console.log({test, response});
            });
          });
    
    打赏 评论

相关推荐 更多相似问题