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'

Norst
  • 23
  • 6

4 Answers4

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')
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • Oh yeah the undefined type. Nice. – 0xFADE Jan 15 '14 at 15:53
  • 1
    He's saying he is getting the text "null" - perhaps he should change his handler to actually return null instead of a string – Kai Qing Jan 15 '14 at 15:53
  • 1
    won't `data` be automatically converted into a `string` ? – Spokey Jan 15 '14 at 15:54
  • 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. – Hanky Panky Jan 15 '14 at 15:55
  • I guess trim() will never catch   Cant think of other legitimate cases of returning a whitespace-like values as opposed to null right now, seems fine. – user1853181 Jan 15 '14 at 16:06
  • You will want to avoid that if your website targets slightly older browsers like IE 8. See [`String.prototype.trim` on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/trim) – user1537366 Jan 15 '14 at 16:10
  • By the way, I just noticed that the `trim` polyfill on MDN (linked above) is wrong. It trims spaces at the beginning and ending of lines even in the middle of the string! – user1537366 Jan 15 '14 at 16:15
  • The trim did the trick. When I looked at the source of my php reply it was: 1: null 2: So I had a blank line in there for some reason. – Norst Jan 15 '14 at 16:19
  • As a side note, I cant express enough my thanks to all of you here who are so ready to help others with issues. I truly thank you all – Norst Jan 15 '14 at 16:22
1

You can try if( !data )

null and 0's are all the same.

0xFADE
  • 832
  • 5
  • 7
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
}
user1853181
  • 813
  • 6
  • 12
0

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

user1537366
  • 1,100
  • 1
  • 9
  • 15