0

I have a javascript ajax request that return's null if nothing is found and then displays that no results were found to the user. This was working fine before but for some reason has stopped. The IF statement no longer seems to catch the null.

//on success
        success: function(data) {

            if(data == 'null') {
                buildaccordion("<h3>No Results Found</h3><div>No Data to Show.</div>");
                return;
            }

When I look at the response from the php side of things, I am indeed being send the text 'null' from the server. I've also checked with the built in debugger in firefox and the value of data is "null". But the if statement does not catch it!?

This was working fine before and I can not see what would be causing this.

I have also tried

== null

=== null

=== 'null'

7
  • 1
    What do alert(data) and alert(typeof data) show? Commented Jan 15, 2014 at 15:50
  • 1
    Have you tried console.log(data) ? Commented Jan 15, 2014 at 15:51
  • possible duplicate of how to check null values in javascript? Commented Jan 15, 2014 at 15:52
  • My bet is there is whitespace console.log(escape(data)); that is why using a format like JSON is better Commented Jan 15, 2014 at 15:55
  • When I look at the response from the php side of things, I am indeed being send the text 'null' from the server Then this observation is in error. How did you determine it? In particular, check for whitespace. Commented Jan 15, 2014 at 15:56

4 Answers 4

4

Since you mentioned its type already shows as string, You can do:

if(data.trim()=='null')


Previous Answer:

When you say if data=='null' you are actually checking if data is the string null

 if(data == 'null') 

Should be

 if(!data) 

Or

if(typeof data === 'undefined')
Sign up to request clarification or add additional context in comments.

9 Comments

Oh yeah the undefined type. Nice.
He's saying he is getting the text "null" - perhaps he should change his handler to actually return null instead of a string
won't data be automatically converted into a string ?
Mmmm if its about a string then his solution should already work. I was confused by the title and answered based on question title rather than the different problem in the body.
I guess trim() will never catch &nbsp; Cant think of other legitimate cases of returning a whitespace-like values as opposed to null right now, seems fine.
|
1

You can try if( !data )

null and 0's are all the same.

Comments

1

In order to be sure (since you do not know precisely what the type of the return value is), better implement a catch-all solution:

if((!data) || (data == 'null') || (typeof data === 'undefined') || (!data.length)) {
    // No results
}

Comments

0

Is there a space somewhere? Maybe if( data.replace(/^\s+/,"").replace(/\s+$/,"")=="null" ) ... will work?

1 Comment

From what I remember, just two years or so in the past, there was no member named trim on a String object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.