2010-11-13 47 views
0

我正在使用下面的代碼。哪個發佈到一個頁面罰款。但是現在我需要從發佈的password.php頁面返回數據。我將如何獲取這些數據?如何從頁面獲取數據ajax jquery

$.ajax({ 
    type: 'POST', 
    url: password.php', 
    data: 'newPassword=' + password + '&userID=<?php print $userID; ?>', 
    success: function(success) { 
        if(success == 1) { 
            $("#Pass").html('The password has been reset.  The temporary password is: <font color=red><b>' +password+'</b></font>');  
        } else { 
            $("#Pass").html('There was an error processing your request. The password was not reset.');  
        } 
    } 
}); 
+0

哪些數據?您的發佈代碼無法使用? – Philip 2010-11-13 17:42:30

回答

2

你已經在你的匿名函數的「成功」變量中找到了它, 。

例如,假設password.php只返回一個包含新密碼的字符串,這是我認爲你正在尋找代碼:

$.ajax({ 
    type: 'POST', 
    url: password.php', 
    data: 'newPassword=' + password + '&userID=<?php print $userID; ?>', 
    success: function(data) { 
     $("#Pass").html('The password has been reset. The temporary password is: <font color=red><b>' + data +'</b></font>'); 
    }, 
    error: function(obj, status, e) { 
     $("#Pass").html('There was an error processing your request. The password was not reset. The error was: ' + status); 
    }) 
}); 

看看jQuery的文檔:http://api.jquery.com/jQuery.ajax/。成功時調用成功函數,如果請求不成功,將調用錯誤函數。

1

success是數據。在決定你想要做什麼之前,你必須首先了解它是什麼。它可以是JSON對象或HTML字符串。

alert(typeof success)alert(success.length)如果它是一個字符串,並做檢查,然後決定你想用它做什麼。

我建議使用JSON作爲password.php頁面,所以響應被分開而不是一個html字符串,這很可能是這裏的情況。

0

確保你在你的php服務器端代碼中回顯。

echo $email; 

return $email;