duanhuan8983 2016-10-11 03:08
浏览 42

POST方法不显示,但GET方法

My POST method does not display on the webpage, but my GET method does even though I haven't create a method with it in my global.js. Does the GET method come with POST? I want my POST to display not GET. How do I do that? I know that my POST work i think because in network (that is in the browser with console), the POST method is there, and the preview prints out the $_POST and $_SESSION. How do I make POST to display on the page instead of GET.

Button.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>Button POST</title>
  </head>
  <body>
       <button id ="postbutton" onclick="location.href='storage.php'">GO</button><br>
  </body>
</html>

storage.php

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

global.js

var oject = [{name:'John', age:17},{name:'James', age:22}];
var json = JSON.stringify(oject);


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

2条回答 默认 最新

  • dongwu5318 2016-10-11 03:23
    关注

    Your GET method is work because you used onclick with location.href method .That will always redirected with GET method so it can display your output ! But to work with POST method in ajax you need to remove onclick method and append return data to body element .

    Button.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>Button POST</title>
      </head>
      <body>
           <button id ="postbutton">GO</button><br>
      </body>
    </html>
    

    global.js

    var oject = [{name:'John', age:17},{name:'James', age:22}];
    var json = JSON.stringify(oject);
    
    
    $(document).ready(function(){
      $('#postbutton').click(function(){
        $('#output').html('sending..');
          var jobject = JSON.stringify(oject);
          console.log(jobject);
          $.ajax({
            method:'post',
            url:'storage.php',
            data:{json:oject},
          })
          .done(function(data){
           $("body").append(data);
        });
      });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline