当我试图检查返回的信息是正确还是错误时,我遇到了问题。在console.log中返回true,但总是输入其他..。
$.ajax({
url: "ajax.php",
type: 'POST',
data: 'act=login&email=' + email + '&password=' + password + '&remember=' + remember,
success: function(data)
{
console.log(data);
if (data.success === true)
{
$("#login-form")[0].reset();
window.location.replace('dashboard.php');
}
else
{
$(".error-message span").html('Please, insert valid data.');
$('.error-message').fadeIn('slow', function () {
$('.error-message').delay(5000).fadeOut('slow');
});
}
}
});谢谢大家。
发布于 2014-01-28 03:07:00
Console.log打印data。
您的IF语句检查`data.uccess‘。这是两个不同的元素。
您是以什么格式从ajax.php发送回数据?
不能假设数据是数组或JSON对象,必须先解析它。
json = JSON.parse(data);
if (json.success === true) {} //or if (json === true) depends on the response from ajax.php根据您的注释,如果返回的数据仅为真或假,只需使用
if (data == true) {//I use double and not triple equality because I do not know if you are returning a string or a boolean.
}你有两个问题:
希望这能有所帮助!
发布于 2014-01-28 03:06:00
返回值可能是字符串true,而不是JavaScript布尔true。
因为您使用的是严格的===比较,这需要相同的变量类型,所以它总是失败的。
与字符串进行比较:
if (data.success == "true")发布于 2014-01-28 03:06:29
如果您的数据看起来像{"success": true},那么在ajax处理程序没有将响应解析为json数据的情况下,数据解析可能是一个问题。而是传递一个字符串。
因此,尝试将dataType属性设置为json,以告诉ajax您正在期待一个json响应。
$.ajax({
url: "ajax.php",
type: 'POST',
data: 'act=login&email=' + email + '&password=' + password + '&remember=' + remember,
dataType: 'json',
success: function (data) {
console.log(data);
if (data.success === true) {
$("#login-form")[0].reset();
window.location.replace('dashboard.php');
} else {
$(".error-message span").html('Please, insert valid data.');
$('.error-message').fadeIn('slow', function () {
$('.error-message').delay(5000).fadeOut('slow');
});
}
}
});https://stackoverflow.com/questions/21396054
复制相似问题