簡體   English   中英

使用jQuery ajax post接收PHP參數

[英]Receive PHP parameters with jQuery ajax post

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

獲取帖子數據的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獲取帖子。 有人可以幫幫我嗎? 謝謝。

如果請求失敗,請將錯誤回調添加到您的$.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)){
    // 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;
}

對此:

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 .success回調中:

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

你真的不必這樣做下一步(jquery確定返回的數據類型確實很好),但很高興在代碼中有它來理解返回的內容。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM