【问题标题】:Receive PHP parameters with jQuery ajax post使用 jQuery ajax post 接收 PHP 参数
【发布时间】:2012-08-02 22:09:02
【问题描述】:

我通过 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"); 
        }
    });
});

获取post数据的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';
}

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

【问题讨论】:

  • 所以假设你的 $.post() 中的 url 是正确的,做 var_dump( $_POST );在您的 php 文件的顶部。你得到了什么?如果那里没有任何内容,请验证您发送到正确的网址。

标签: php jquery ajax post


【解决方案1】:

如果请求失败,请将错误回调添加到您的 $.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);
    }
});


更新

改变这个:

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

到这里:

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;
}

现在在你的 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); 

},

您实际上不必再执行此操作(jquery 可以很好地确定返回的数据类型),但很高兴在代码中包含它以了解返回的内容。

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

【讨论】:

  • 感谢您的帮助。我没有得到错误捕捉工作。我确实看到了我对 PHP 文件的萤火虫响应。数组([proj_name] => 项目名称:8.3 PRDx 未保存草稿 [date] => 日期:2012-08-03 [req_comp_date] => 请求完成日期:n/a [status] => 状态:等待 [secondUserID] => 分配另一个用户来编辑这个项目请求文档? Nilo Zaratan )所以,我只需要以某种方式将它输出到页面。它没有正确回显。
猜你喜欢
  • 1970-01-01
  • 2013-07-28
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
  • 2012-09-21
  • 1970-01-01
  • 2014-02-14
  • 2018-11-18
相关资源
最近更新 更多