ajax成功:我无法检查返回的信息是对还是错 [英] ajax sucess: i can not check if the returned information is true or false

查看:77
本文介绍了ajax成功:我无法检查返回的信息是对还是错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试检查返回的信息是正确还是错误时,我遇到了问题.在console.log中返回true,但始终输入else ...

I have a problem when I try to check if the returned information is true or false. In console.log returns true but always enters else ...

$.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');
                            });
                        }
                    }
                });

谢谢大家.

推荐答案

Console.log显示data.

Console.log prints data.

您的IF语句检查`data.success'.那是两个不同的元素.

Your IF statement checks `data.success'. Those are 2 different elements.

您以什么格式从ajax.php发送回数据?

In what format are you sending the data back from ajax.php?

您不能假定数据是数组或JSON对象,必须首先对其进行解析.

You can't assume that data is an array or JSON object, you have to parse it first.

json = JSON.parse(data);

if (json.success === true) {} //or if (json === true) depends on the response from ajax.php

根据您的评论,如果返回的数据仅是true或false,则只需使用

Per your comment, if you returned data is only true or false, simply use

if (data == true) {//I use double and not triple equality because I do not know if you are returning a string or a boolean.
}

您有2个问题:

  1. 您正在尝试访问data.success而不是仅访问数据.
  2. 您正在使用严格的比较(===),但我真的不确定您返回的数据是否是布尔值而不是字符串. @Pekka的答案也提到了它(但是由于data.success不存在,所以它不起作用).

希望这会有所帮助!

这篇关于ajax成功:我无法检查返回的信息是对还是错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆