0

使用.ajax()时,jQuery 似乎没有传递表单变量或 http POST 操作

做一个var_dump( $_POST ); 结果是一个空数组。

建议?我做错了什么......这是一个简单的概念 - 有人发布了类似的 Q,但没有人回应,他的解决方案是在表单中添加一个 id,我的已经有了,所以它没有解决我的问题。

目前我正在firebug中查看返回页面内容 - 我还没有做任何事情,因为我无法将数据发送到服务器......(首先要做的事情)。

我的 HTML

<script type="text/javascript">
$( document ).ready( function(){
    $( "#myform" ).submit( function () {
        $.ajax({
            type: "POST",
            dataType: "html",
            cache: false,
            url: "x.php",
            success: function( data ){              
                //...tell user there was success...
            },
            error: function( data ){
                //...tell user thre was a problem...
            }
        });
        return false;
    });     
});
</script>

<form name="myform" id="myform" action="" method="POST">
    <label for="name" id="name_label">x: </label>
    <input type="text" name="x" id="x" size="30" value=""/> 
    <input type="submit" name="submit" value="Submit"> 
</form>
<div id="results"><div>

这是(x.php)PHP代码 - 非常简单的概念证明......

<?php
var_dump( $_GET );
var_dump( $_POST );
var_dump( $_REQUEST );
?>

响应看起来像这样

array(0) {}
array(0) {}
array(1) {
  ["PHPSESSID"]=>
  string(26) "e1j18j..."
}
4

3 回答 3

5

你没有data在ajax调用中传递任何帖子,试试

    $.ajax({
        type: "POST",
        dataType: "html",
        cache: false,
        url: "x.php",
        data: $(this).serialize(),
        success: function( data ){              
            //...tell user there was success...
        },
        error: function( data ){
            //...tell user thre was a problem...
        }
    });
于 2013-01-24T06:33:47.767 回答
1

您需要序列化表单:

    $.ajax({
        type: "POST",
        data: $(this).serialize(),
        cache: false,
        url: "x.php",
        success: function( data ){              
            //...tell user there was success...
        },
        error: function( data ){
            //...tell user thre was a problem...
        }
    });

没有data,您不会通过 POST 请求发送任何内容。

于 2013-01-24T06:33:52.947 回答
0

你可以这样做

$(document).ready(function(){
   $( "#myform" ).submit( function () {
       $.ajax({
         type:'POST',
         data:'xname='+$("#x").val(),
         url:'SOMETHING.php',
         success:function(data){
           //Do something with the returned data
         }
       });
   });
});

并在 php 文件中

echo $_POSST['xname'];

希望这可以帮助

于 2013-01-24T06:43:21.070 回答