jQuery将整个页面返回给Ajax

jQuery returns full page to Ajax

本文关键字:返回 Ajax jQuery      更新时间:2023-09-26

我看到了一个链接,但仍然对为他们提供的答案感到困惑:jQuery Ajax返回整个页面的

我的情况也差不多。

我有一个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.

我不知道为什么。):

这里也发生了同样的情况,我发现它与其他插件有冲突,停用你的插件,它应该可以工作。

希望它能帮助其他人寻找这个解决方案。

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

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

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

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

也许这也能解决你的问题。。。