使用PHP的jQuery AJAX响应很难

问题描述:

I am fairly new to AJAX. I am trying to get a simple login script to work. This is using jQuery 1.6.4. In the AJAX below, when the user clicks a button, it sends the email address and password to login.php. That all seems to work fine. The trouble is with the success function. When the email and password are correct, it should return true. It should return false when it does not work. Using Firebug, I see that it works with console.log. It also works correctly if I write alert(response);. However, the conditional always evaluates to false even when response is equal to true. I've tried both if(response=="true") and if(response==="true"), putting the variable outside the function, and a few other things without success. Would anyone have any ideas on how to fix this?

Thank you for any help or ideas, Jason.

AJAX:

$("#firstpage").live('pageinit', function (evt) {
$('#button').click(function(){       
var $form = $('#login'),
$inputs = $form.find("input"),
serializedData = $form.serialize();
$.ajax({
  type: 'POST',
  url: 'php/login.php',
  data: serializedData,
  success: function(response){
    console.log("Response: "+response);
    if(response=="true") 
    {
$('#firstpage #wrong').text("Login script is working");
} else {
$('#firstpage #wrong').text("Your email and password combination did not match.");
}

    },      
  dataType: 'json'
});
});  
});

If it helps, this is my login.php script.

$email = $_POST['email'];
$password = $_POST['password'];
require_once("DB.php");
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!"); 
mysql_select_db($db) or die ("Unable to select database!"); 

$query = "SELECT * FROM member WHERE email='$email' AND password='".md5($_POST['password'])."'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error()); 

$num_rows = mysql_num_rows($result);
if($num_rows>0){
$output = true;
} else {
$output = false;
}
echo json_encode($output);

我是AJAX的新手。 我试图让一个简单的登录脚本工作。 这是使用jQuery 1.6.4。 在下面的AJAX中,当用户单击按钮时,它会将电子邮件地址和密码发送到login.php。 这一切似乎都很好。 问题在于成功功能。 当电子邮件和密码正确时,它应该返回true。 当它不起作用时它应该返回false。 使用Firebug,我发现它适用于console.log。 如果我写警报(响应);它也可以正常工作。 但是,即使response等于true,条件总是计算为false。 我已经尝试了 if(response ==“true”) code>和 if(response ===“true”) code>,将变量放在函数外面,还有一些 其他事情没有成功。 有没有人对如何解决这个问题有任何想法? p>

感谢您提供任何帮助或想法, Jason。 p>

AJAX: p>

  $(“#firstpage”)。live('pageinit',function(evt){
 $('#button')。click(function(){
var $ form =  $('#login'),
 $ inputs = $ form.find(“input”),
serializedData = $ form.serialize(); 
 $ .ajax({
 type:'POST',
  url:'php / login.php',
 data:serializedData,
 success:function(response){
 console.log(“Response:”+ response); 
 if(response ==“true”)  
 {
 $('#firstpage #wrong')。text(“登录脚本正在运行”); 
}其他{
 $('#firstpage #wrong')。text(“您的电子邮件和密码组合 不匹配。“); 
} 
 
},
 dataType:'json'
}); 
}); 
}); 
  code>  pre> 
  
 

如果有帮助,这是我的login.php脚本。 p>

  $ email = $ _ POST ['email']; 
 $ password = $ _POST [  'password']; 
require_once(“DB.php”); 
 $ connection = mysql_connect($ host,$ user,$ pass)或die(“Unable to connect!”);  
mysql_select_db($ db)或die(“无法选择数据库!”);  
 
 $ query =“SELECT * FROM member WHERE email ='$ email'AND password ='”。md5($ _ POST ['password'])。“'”; 
 $ result = mysql_query($ query) 或者死(“查询中的错误:$ query。”。mysql_error());  
 
 $ num_rows = mysql_num_rows($ result); 
if($ num_rows> 0){
 $ output = true; 
} else {
 $ output = false; 
} 
echo json_encode($ output)  ); 
  code>  pre> 
  div>

The response is an object because you have "dataType: 'json'". jQuery will try and convert the responseText to JSON. If you need to check the data the server returned, try using

if (response === true) {

}

or simply

if (response) {

}

or just have jQuery return the string by removing the datatype: 'json'

Don't use quotations with an exactly-equals sign ===. Use

if (response === true)

Because your PHP script returns true, not "true".

I'm not avid with PHP, but try removing the quotes around true.