代码之家  ›  专栏  ›  技术社区  ›  TigerTiger

jquery.ajax method=“post”但是$post为空

  •  11
  • TigerTiger  · 技术社区  · 15 年前
    $.ajax({
        method: "post"
        , url: "save.php"
        , data: "id=453&action=test" 
        , beforeSend: function(){
    
        } 
        , complete: function(){ 
        }  
        , success: function(html){ 
            $("#mydiv").append(html);        
        }
    });
    

    我已经将方法类型设置为post,但是在save.php中,我只是在 $_GET $_REQUEST 但不在 $_POST .

    我的表格看起来像:

    <form method="post" id="myform" action="save.php">
    

    它不起作用,环顾四周,在谷歌上,尝试添加 enctype

    <form method="post" id="myform" action="save.php" enctype="application/x-www-form-urlencoded">
    

    但仍然 美元邮报 空的?

    我该怎么做?

    5 回复  |  直到 6 年前
        1
  •  9
  •   Waleed Amjad    15 年前

    而不是 method: "post" 你需要使用 type: "POST"

    因此,这应该可以在不更改表单html的情况下工作:

    $.ajax({
        type: "POST"
        , url: "save.php"
        , data: "id=453&action=test" 
        , beforeSend: function(){
    
        } 
        , complete: function(){ 
        }  
        , success: function(html){ 
            $("#mydiv").append(html);        
        }
    });
    

    不知道为什么不管用,但这对我很管用:

    小精灵

    <?php
    print_r($_POST);
    

    文件HTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
        <title>Example</title>
    
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
        <script type="text/javascript">
            $(function() {
                $('input').click(function() {
                    $.ajax({
                        type: "POST"
                        , url: "save.php"
                        , data: "id=453&action=test" 
                        , beforeSend: function(){
    
                        } 
                        , complete: function(){ 
                        }  
                        , success: function(html){ 
                            alert(html);        
                        }
                    });
                });
            });
        </script>
    </head>
    
    <body>
        <div id="main"><input type="button" value="Click me"></div>
    </body>
    </html>
    

    你的错误一定在别的地方。

        2
  •  4
  •   Community kfsone    7 年前

    为什么不打电话 jQuery.post() 直接?

    $.post("save.php",
      $("#myform").serialize(),
      function(html) { 
        $("#mydiv").append(html);        
      },
      "html"
    );
    

    关于 jQuery.ajax() ,更改为 type: "POST" 而不是 method: "POST" 会导致 POST 请求:

    $.ajax({
            type: "POST",
            url: "test.mhtml",
            data: $("#myform").serialize(),
            success: function(html){ 
                    $('#mydiv').html(html);
            }
    });
    

    这在apache日志中显示为:

    ::1 - - - [30/Oct/2009:09:44:42 -0700] "POST /test.php HTTP/1.1" 200 9 "http://localhost:10501/test.mhtml" "(sic)"
    

    可能的替代问题:
    我发现 this question on StackOverflow 同时看看你的问题。也许不是jquery给您带来麻烦,而是php?排名靠前的答案提供了一些建议,以确保php不受干扰,排名第二的答案提供了一些代码来查看请求是否是 与否:

    <?php
      if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        echo 'POSTed';
      }
    ?>
    
        3
  •  4
  •   jkofron.e    7 年前

    我也对在混合中丢失的变量有困难,在使用重写条件重定向外部重定向时发现了这一点。 dir/foo.php dir/foo Apache完全放弃POST请求并重定向。

    因此,在编写JavaSRIPT时,必须引用该文件,这样它就可以绕过任何外部重定向。

    删除与javascript内联的扩展名。

    url: "dir/foo"

    而不是

    url: "dir/foo.php"

    我希望这能帮助像我这样找到这一页的人。

        4
  •  3
  •   Rajnish    8 年前

    **成功前在代码中添加以下代码**

    beforeSend: function(xhr){xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")}
    
        5
  •  0
  •   charlesdeb    6 年前

    我也经历过类似的问题 $(form).ajaxSubmit() .

    浏览器工具指示我成功发布了 POST . 在服务器上 $_SERVER['REQUEST_METHOD'] 也表示了 ,但是 $_POST PHP中的变量被返回为 array(0) { } . 我的问题是我实际上提交了一个空的表格。一旦表格中有数据, 美元邮报 里面有东西(正如你所料!)但更重要的是, if ($_POST) { ... } 开始返回 true .