weixin_33720452 2016-10-10 11:57 采纳率: 0%
浏览 6

无法将对象发送到php

I wrote a script to test out post and session using ajax to send the object to php so I can store it into session, however, when I click on the button that redirects me into the footer.php, the webpage will echo false. On the network, it says that the objects has been post into storage.php. Don't know why it would say false on the page. Sometimes when I go back and forth between the pages, the console will print out [{"name":"John","age":17},{"name":"James","age":22}]True

Would like to know what is wrong with my code.

End to End I like the object to able to post and store it into session.

header.php

<!doctype html>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src ="global.js"></script>
    <title>Post Experiment</title>
  </head>
  <body>
       <button id ="jButton" onclick="location.href='footer.php'">GO</button><br>
  </body>
</html>

storage.php

<?php
if(isset($_POST['json'])){
  $_SESSION['object'] = $_POST["json"];
  echo $_POST["json"];
  echo 'True';
}else {
  echo 'False';
}
?>

footer.php

<?php
  session_start();
  include('storage.php');
?>
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script src ="global.js"></script>
    <title>The reciever</title>
  </head>
  <body>
       <?php //echo $_SESSION['object']; ?>
  </body>
</html>

global.js

var oject = [{name:'John', age:17},{name:'James', age:22}]

$(document).ready(function(){
  $('#showcart').click(function(){
    $('#output').html('sending..');
      var json = JSON.stringify(oject);
      $.ajax({
        method:'post',
        url:'storage.php',
        data:{json:json},
      })
      .done(function(data){
        console.log(data);
    });
  });
});

EDIT: Ok, looking through the network in the browser, I clicked on Response and it said

    Array
(
    [json] => Array
        (
            [0] => Array
                (
                    [name] => John
                    [age] => 17
                )

            [1] => Array
                (
                    [name] => James
                    [age] => 22
                )

        )

)
True

However, it doesn't say this in the actual console. I think it suppose to look like this when I echo it out.

EDIT 2:

Ok, looking at the Network, the method POST on storage says Array ( [json] => Array ( [0] => Array ( [name] => John [age] => 17 ) [1] => Array ( [name] => James [age] => 22 ) ) ) True. However, on footer.php, its a GET method and prints out Array ( ) False the same as the webpage.

  • 写回答

0条回答 默认 最新

    报告相同问题?