使用jQuery抛出空错误的Ajax POST

时间:2015-02-04 20:41:24

标签: javascript php jquery ajax

尝试修复以下简单代码:

$(document).ready(function()
{
    $("#login").click(function()
    {
        var a = 3;
        var b = 2;
        var dataString = "a="+a+"&b="+b;
        console.log(dataString);
        $.ajax({
                    url: "ajax.php",
                    type: "post",
                    data: dataString,
                    cache:"false",
                    beforeSend: function(){console.log("Sending...");},
                    error: function(x,y,z){console.log("err: "+x+" "+y+" "+z);},
            success: function(x,y,z){console.log("succ: "+x+" "+y+" "+z);}
                });
    });
}); 

ajax.php:

<?php
$a = $_POST['a'];
$b = $_POST['b'];

echo $a+$b;
?>

我的问题是,网站总是返回错误并清空x,y,z。

1 个答案:

答案 0 :(得分:0)

    $(document).ready(function()
    {
        $("#login").click(function()
        {
            var a = 3;
            var b = 2;
            var dataString:={a:a,b:b};
            $.ajax({
                    url: "ajax.php",
                    type: "post",
                    data: dataString,
                    success:function(result)                 {
result=jQuery.parseJSON(result);
console.log("OurResult:"+result );
                        }
                    });
        });
    }); 

现在是php部分:

<?php
$a = $_POST['a'];
$b = $_POST['b'];
$table=array($a,$b);
echo json_encode($table);
?>