如何从页面ajax jquery中获取数据

时间:2010-11-13 17:38:13

标签: jquery-ui jquery

我正在使用以下代码。哪个页面正好发布。但现在我需要从发布的password.php页面返回数据。我如何获得这些数据?

$.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.'); 
        }
    }
});

3 个答案:

答案 0 :(得分: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/。成功时调用success函数,如果请求不成功,将调用error函数。

答案 1 :(得分:1)

success是数据。在决定要用它做什么之前,你必须首先找出它是什么。它可以是JSON对象或HTML字符串。

alert( typeof success)alert( success.length)如果是一个字符串,并进行检查,然后决定你要用它做什么。

我建议在password.php页面上使用JSON,因此响应被分割而不是html字符串,这很可能是这里的情况。

答案 2 :(得分:0)

确保你在php服务器端代码中回显。

echo $email;

不是

return $email;