我只想知道如何比较 JavaScript 中 $.ajax 调用产生的回声。我尝试了这个,即使我得到1
了,它实际上并没有正确比较结果。
jQuery:
$.ajax({
type: "POST",
url: "login.php",
data: user,
dataType: 'html',
success: function(result)
{
alert(result);
if(result == '1')
{
alert("logged in :D");
//document.location.replace('home.php');
}
else
{
alert("not logged in :<");
}
},
failure: function()
{
alert('An Error has occured, please try again.');
}
});
PHP:
<?php
session_start();
$host = "localhost";
$user = "root";
$passw = "";
$con = mysql_connect($host, $user, $passw);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$json = $_REQUEST['json'];
$json = stripslashes($json);
$jsonobj = json_decode($json);
$password = $jsonobj->password;
$email = $jsonobj->email;
mysql_select_db("tinyspace", $con);
$result = mysql_query("SELECT 1 FROM users WHERE email = '"
. $email . "' AND password = '" . $password . "'");
while($info = mysql_fetch_array( $result ))
{
if($info[0] == 1)
{
echo '1';
}
}
?>