admin管理员组

文章数量:1404923

I'm using AngularJS as my framework and also connect to a webservice to fetch some JSON data. I have the following code:

$login.onlineLogin($scope.credentials).
    success(function (data, status, headers, config) {

        if (data !== null ) {
            console.log('Successful Login!');
            console.log('Returned data: ' + data);
        } else {
            console.log('Failed to Login...');
        }

    }).
    error(function (data, status, headers, config) {
        console.log('HTTP Error...');
    });

Now, when the HTTP call es back the status could still be 200:OK but the object that it returns could be null, in the case where the user provided the wrong user credentials for example. So what I'm expecting to happen in that case is that if the object is NOT null, it'll print out 'Successful Login!' and then print out the object.

However, what's happening now is that I use some fake credentials, so the webservice returns null. But still the 'Successful Login!' gets printed, and then after it prints 'Returned data: null'... Which doesn't make sense. If the data is null, it should never have even ended up in that code block?

I'm using AngularJS as my framework and also connect to a webservice to fetch some JSON data. I have the following code:

$login.onlineLogin($scope.credentials).
    success(function (data, status, headers, config) {

        if (data !== null ) {
            console.log('Successful Login!');
            console.log('Returned data: ' + data);
        } else {
            console.log('Failed to Login...');
        }

    }).
    error(function (data, status, headers, config) {
        console.log('HTTP Error...');
    });

Now, when the HTTP call es back the status could still be 200:OK but the object that it returns could be null, in the case where the user provided the wrong user credentials for example. So what I'm expecting to happen in that case is that if the object is NOT null, it'll print out 'Successful Login!' and then print out the object.

However, what's happening now is that I use some fake credentials, so the webservice returns null. But still the 'Successful Login!' gets printed, and then after it prints 'Returned data: null'... Which doesn't make sense. If the data is null, it should never have even ended up in that code block?

Share Improve this question edited Aug 25, 2015 at 6:39 Chris Martin 30.8k12 gold badges80 silver badges141 bronze badges asked Aug 25, 2015 at 6:30 user818700user818700 8
  • 1 use if (data) {} else {} instead – Filippo oretti Commented Aug 25, 2015 at 6:32
  • Same thing is happening – user818700 Commented Aug 25, 2015 at 6:33
  • 2 maybe the data returns a string not null but 'null'. try to use typeOf. – kdlcruz Commented Aug 25, 2015 at 6:33
  • yes console.log(typeof data); – Filippo oretti Commented Aug 25, 2015 at 6:33
  • try if (data != null) or that's just php, just try )) – MozzieMD Commented Aug 25, 2015 at 6:33
 |  Show 3 more ments

3 Answers 3

Reset to default 3

Instead of

if (data !== null ) {
    .....
}

use this

if(data) {
    ....
}

This will check for empty strings (""), null, undefined, false and the numbers 0 and NaN.

If you're not sure what is the type of your variable use.

console.log(typeof variableName)

In your case, your problem can be fixed using

if (data != 'null') {
...

there is difference between == and ===. JavaScript has both strict(===) and type-converting(==) equality parison.

Two strings are strictly equal when they have the same sequence of characters, same length, and same characters in corresponding positions. Two numbers are strictly equal when they are numerically equal (have the same number value). NaN is not equal to anything, including NaN. Positive and negative zeros are equal to one another. Two Boolean operands are strictly equal if both are true or both are false. Two objects are strictly equal if they refer to the same Object. Null and Undefined types are == (but not ===). [I.e. Null==Undefined (but not Null===Undefined)]

if (data != null ) {
          console.log('Successful Login!');
          console.log('Returned data: ' + data);
        } else {
          console.log('Failed to Login...');
        }

本文标签: angularjsJavascript null checking not working as expectedStack Overflow