jQuery将整页返回给Ajax

时间:2012-03-17 13:52:22

标签: javascript jquery

这是我看到的链接,但仍然对为他们提供的答案感到困惑:jQuery Ajax returns the whole page

我的情况类似。

我有一个index.php,它调用一个弹出式登录表单,尝试将此登录信息提交给另一个名为login_check.php的php的ajax调用。

这是我的index.php:

var dataString = 'email='+ email.val() + '&password=' + password.val();

$.ajax({
type: "POST",
url: "login_check.php",
data: dataString,
cache: false,
error: function (request, status, error) {
    alert("An error occured while trying to complete your request: " + error);
},
success: function(data)
{
    alert(data);

}
}); 

这是我的login_check.php中的内容:

session_start();

//input from login form 
$myemail = htmlspecialchars(trim($_POST['email']));
$mypassword = htmlspecialchars(trim($_POST['password']));

//selecting from database to check user account
$sql="SELECT * FROM user WHERE email='$myemail' and password='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$count=mysql_num_rows($result);

//If result matched $myemail and $mypassword, table row must be 1 row 
if($count==1)
{   
    echo 'true';
    session_register("myemail");
    $_SESSION['login_user']=$myemail;
}   
else if($count==0)
{
    echo 'false';
}

警报框的输出是我当前所在页面的完整html,即index.php。

我不知道为什么。 ):

3 个答案:

答案 0 :(得分:0)

您可能还想尝试这种发出Ajax POST请求的语法。

$.post('login_check.php',{'email':email.val(),'password':password.val()},function(response){
  alert(response);
},"json");

答案 1 :(得分:0)

不知道这是否能解决您的问题,但编码数据总是一个好主意:

var dataString = encodeURIComponent('email='+ email.val() + '&password=' + password.val());

也许这也解决了你的问题......

答案 2 :(得分:0)

同样发生在这里,我发现它与其他插件有冲突,停用你的插件,它应该有用。

希望它有助于其他寻找此解决方案。