简体   繁体   English

jQuery的ajax不传递数据到PHP?

[英]jquery ajax not passing data to php?

I am trying to pass javascript Date object to php, but when i dump $_POST it returns empty array or string. 我试图将javascript Date对象传递给php,但是当我转储 $ _POST时,它将返回空数组或字符串。 I am doing this in same file, so in file.php are php code and javascript code. 我在同一文件中执行此操作,因此在file.php中是php代码和javascript代码。 Also javascript shows that everything is correct because it alert in success function. javascript也显示一切正确,因为它会警告成功功能。

<?php
var_dump($_POST); //returns empty array
var_dump($_POST['zone']); //returns null
?>

<script type="text/javascript">
  var date = new Date();
  $.ajax({
      type:'POST',
      data: {zone : date},
      success: function(data){
         alert("test"); //this runs normally
      },
      error: function(xhr, textStatus, error){
            console.log(xhr.statusText);
            console.log(textStatus);
            console.log(error);
      }     
  });   
</script>

Here is some code I tested on phpfiddle.org: 这是我在phpfiddle.org上测试过的一些代码:

<?php
if(isset($_POST['zone'])){
    // Only runs when there is a Post for 'zone'
    //var_dump($_POST);
    echo $_POST['zone'];
} else{
?>
<html>
    <head>
        <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    </head>
    <body>
        <script type="text/javascript">
            var date = new Date();
            function postZone(){
                $.ajax({
                    type:'POST',
                    data: {zone : date},
                    success: function(data){
                        console.log(data);
                        //alert("test"); //this runs normally
                        $("#zone").html(data.substring(data.indexOf("(")) + " is client TimeZone.");
                    },
                    error: function(xhr, textStatus, error){
                        console.log(xhr.statusText);
                        console.log(textStatus);
                        console.log(error);
                    }     
                });   
            }
            postZone();
        </script>
        Running Post.
        <div id='zone'></div>
    </body>
</html>
<?php
}
?>

Here is what I saw in the Post results: 这是我在发布结果中看到的内容:

array(1) {
  ["zone"]=>
  string(57) "Mon Jan 18 2016 09:44:17 GMT-0800 (Pacific Standard Time)"
}
string(57) "Mon Jan 18 2016 09:44:17 GMT-0800 (Pacific Standard Time)"

Adding the if statement helps to not confuse the results. 添加if语句有助于避免混淆结果。 Not sure how you'll do this if you're including this code. 如果要包含此代码,则不确定如何执行此操作。 As I said in my comment, you;re not passing the Date object in your Post, so you should pass the specifics of what you need to PHP. 正如我在评论中说的那样,您没有在Post中传递Date对象,因此您应该将所需的细节传递给PHP。

Edit 编辑

Updated code. 更新的代码。 You can now see the data in console and the results show are: 现在,您可以在控制台中查看data ,结果显示为:

Running Post.
(Pacific Standard Time) is client TimeZone.

This of course will be different for each client. 对于每个客户,这当然是不同的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM