TrySpace
TrySpace

Reputation: 2460

$.ajax check if return string is 'ERROR'

I'm trying to check the value returned from $.ajax is a string 'ERROR', that I've echoed back from PHP.

if (this.data === "ERROR");
{ 
   return { 'bla' : 'bla' };
}

But it will trigger the if every time.. also with ==

I tried:

   if (typeof(this.data) == 'string')

Seems to work, but still have the problem of returning an object when it's 'undefined', tried:

typeof(this.data) === 'string' || this.data=== 'undefined' 

Tried all kinds of combinations, but simply said, what I want to do, When there is not nicely formatted JSON, but only 'ERROR', then I want to give in my own default JSON object and continue, but for some reason the if statement will be true every time.

There must be a simpler way for this.

And I can't work with the error: or succes: 'classes' of $.ajax, because It's a custom error message I return.

Upvotes: 0

Views: 249

Answers (2)

Miguel Borges
Miguel Borges

Reputation: 7669

if (this.data === "ERROR") { 
   return { 'bla' : 'bla' };
}

Upvotes: -1

Matt Ball
Matt Ball

Reputation: 360046

You've got an extra semicolon:

//                        ↓ here
if (this.data === "ERROR");
//                        ↑ here

Remove it.

Upvotes: 6

Related Questions