【发布时间】:2012-03-17 13:52:22
【问题描述】:
这是我看到的一个链接,但仍然对为他们提供的答案感到困惑: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。
我不知道为什么。 ):
【问题讨论】:
-
尝试在发送
GET参数时将Ajax 调用类型更改为GET。您还需要更改 PHP 代码以使用$_GET变量。 -
请尝试将问题缩小到客户端或服务器端。对于客户端,您可以使用 chrome 开发人员工具检查传出的 ajax 调用。对于服务器端,您可以向自己发送一封包含整个
$_REQUEST数组的电子邮件... -
jquery 看起来不错,所以我猜问题是 php 页面返回了一个完全呈现的响应。
-
我刚刚意识到如果我只将 放在我的 login_check.php 中,它不会向我的 ajax 返回任何内容。 ): 不知道为什么。
标签: javascript jquery