0

I'm issuing a strange comportment using an ajax request with jquery, in the code below the header.STATUS can't be retrieved in a variable called 'status' it seems to work fine with another variable name, I'd like to know why ?

$.ajax({ 
     type: "GET", 
     url: myConnect.prototype.URL, 
     data: params, 
     error: this.errorHandler,
     success: function(data){ 
          response = JSON.parse(data);

          status = response.STATUS;
          if(status.SUCCESS){ //never true since status is not an object.
               console.log('success !');
          }
     }
});

data is : {"STATUS":{"SUCCESS":1,"DESCRIPTION":""}}

the status variable is set as a string, I can see its value in the chrome's Watch Expression view :

status: "[object Object]"

but if I call this variable 't' (or anything else) the condition will be true and it will log 'success !'

any explanation ?

EDIT : the response parsed object as displayed by chrome :

 response : Object
      STATUS: Object
           DESCRIPTION: ""
           SUCCESS: true
      _proto_: Object
 _proto_: Object
5
  • 2
    note that data does not exist... you called it dara in your function param Commented Mar 20, 2013 at 14:41
  • 1
    First, set your dataType to "json" and remove JSON.parse, let jQuery do the parsing for you. Though, in theory that shouldn't make any noticiable difference to the result. What do you get if you console.log data after it is parsed as an object? Commented Mar 20, 2013 at 14:44
  • Do you have a fiddle example? Commented Mar 20, 2013 at 14:45
  • Looks like our friend is implicitly declaring global variables in the success callback. That is not a great idea. Commented Mar 20, 2013 at 14:49
  • sry for the mistake on 'data' and thanks for all your comments. Commented Mar 20, 2013 at 14:52

1 Answer 1

2

If you make status local it should work

$.ajax({ 
     type: "GET", 
     url: WellcomsConnect.prototype.URL, 
     data: params, 
     error: this.errorHandler,
     success: function(dara){ 
          response = JSON.parse(data);

          var status = response.STATUS; // make it local
          if(status.SUCCESS){ //never true since status is not an object.
               console.log('success !');
          }
     }
});

EDIT:

This explanation below is actually in Chrome only - Firefox works fine - IE9 doesn't work

the reason it's not is because window has a status property already and your Status object is getting stored as a String - [object Object] instead of an object

enter image description here

I tested this in IE9/Firefox/Chrome

Only working if Firefox - using global

Then

Working in all - using local

Sign up to request clarification or add additional context in comments.

1 Comment

Hehe I knew not having var would cause an issue. Nice work.

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.