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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效