使用 jQuery ajax post 接收 PHP 参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11819791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:11:07  来源:igfitidea点击:

Receive PHP parameters with jQuery ajax post

phpjqueryajaxpost

提问by Dan Thach

I am sending data via jQuery's .ajax method to my PHP file. Both files are on the same domain. The file making the post looks like this..

我通过 jQuery 的 .ajax 方法将数据发送到我的 PHP 文件。两个文件都在同一个域中。制作帖子的文件看起来像这样..

$('#pdf').click(function() {                    
    var proj_name = $('#proj_name').text();
    var date = $('#date').text();
    var req_comp_date = $('#req_comp_date').text();
    var status = $('#status').text();
    var secondUserID = $('#secondUserID').text();

    var postData = {
        "proj_name" : proj_name,
        "date" : date,
        "req_comp_date" : req_comp_date,
        "status" : status,
        "secondUserID" : secondUserID,
    };

    console.log(postData);

    $.ajax({
        type: "POST",
        url: "test.php",
        data: postData, 
        success: function(){
            alert(proj_name + ' ' + status);
            window.open("test.php"); 
        }
    });
});

And the PHP file getting the post data is...

获取帖子数据的PHP文件是......

//request parameters
$proj_name = $_POST['proj_name'];
$date = $_POST['date'];
$req_comp_date = $_POST['req_comp_date'];
$status = $_POST['status'];
$secondUserId = $_POST['secondUserId'];

echo 'postData: ' . var_dump($_POST);

if ($_POST)){
    echo $proj_name;
    echo $date;
    echo $req_comp_date;
    echo $status;
    echo $secondUserId;
} else {
    echo 'problem';
}

In my firebug console, I can see that the parameters posted with .ajax, but I cannot get the post via PHP. Can anyone help me out please? Thank you.

在我的 firebug 控制台中,我可以看到使用 .ajax 发布的参数,但我无法通过 PHP 获取该发布。有人可以帮我吗?谢谢你。

回答by Anil

Add the error callback to your to your $.ajaxcall to debug if the request is failing.

$.ajax如果请求失败,则将错误回调添加到您的调试调用中。

$.ajax({
    type: "POST",
    url: "test.php",
    data: postData, 
    success: function(){
        alert(proj_name + ' ' + status);
        window.open("test.php"); 
    },
    // Alert status code and error if fail
    error: function (xhr, ajaxOptions, thrownError){
        alert(xhr.status);
        alert(thrownError);
    }
});



更新

Change this:

改变这个:

if ($_POST)){
    echo $proj_name;
    echo $date;
    echo $req_comp_date;
    echo $status;
    echo $secondUserId;
} else {
    echo 'problem';
}

To this:

对此:

if ($_POST)){
    // Make a array with the values
    $vals = array(
        'proj_name'     => $proj_name,
        'date'          => $date,
        'req_comp_date' => $req_comp_date,
        'status'        => $status,      
        'secondUserId'  => $secondUserid
    );

    // Now we want to JSON encode these values to send them to $.ajax success.
    echo json_encode($vals);

    exit; // to make sure you arn't getting nothing else

} else {
    // so you can access the error message in jQuery
    echo json_encode(array('errror' => TRUE, 'message' => 'a problem occured'));
    exit;
}

Now in your jQuery .successcallback:

现在在您的 jQuery.success回调中:

success: function(data){ // Our returned data from PHP is stored in "data" as a JSON Object
    alert(data.req_comp_date); // access your returned vars like this.
    // data.date; // is your posted date.. etc
    alert(data.proj_name + ' ' + data.status);
    window.open("test.php"); 

    // You can also get your error message like so..
    if(data.error) // if its true, we have a error, so display it.
         alert('ERROR: ' + data.message); 

},

You dont really have to do this next bit (jquery does a good job of determining the data type returned), but its nice to have it in the code to understand what is being returned.

你真的不必做这下一点(jquery 在确定返回的数据类型方面做得很好),但很高兴在代码中使用它来了解返回的内容。

$.ajax({ ...
    type: "POST",
    url: "test.php",
    data: postData, 
    dataType: "json" // <-- Add this to tell jquery, we are being returned a JSON object.
.... });