4

I have an ajax function that sends an email to somewhere and receives a response from the server from a json object with type = either success or error

$("#submit_btn").click(function(event) { 
    event.preventDefault();

    var post_data = {
        'email': $("#email").val()
    };
    $.post( "sendEmail.php", post_data ).done(function(response){
        if(response.type == 'success'){
            $("#sentmail").fadeIn("slow")
            setTimeout(function(){
                $("#mail").val("Enter your email here");

                $("#sentmail").fadeOut("slow")  
            },3000);
        }
        else{
            $("#sentmailfail").fadeIn("slow")
            setTimeout(function(){
                $("#mail").val("Enter your email here");

                $("#sentmailfail").fadeOut("slow")  
            },3000);
        }
    },"json")
});

The interesting part is that if I console.log(response) I get for instance {"type":"success","desc":"something"} and then straight after that console.log( (response.type == "error") ) // TRUE

if I take the consoled log from response and assign it to a variable for instance a = {"type":"success","desc":"something"} then a.type == "error" is false.

Can someone explain this?

Nope
  • 22,147
  • 7
  • 47
  • 72
  • 1
    The question I have is *when* do you try a `console.log(response)`? – amphetamachine Aug 28 '14 at 22:02
  • 1
    `{"type":"success"` ... `type == "error"` um... you're clearly missing something, because your example is completely off. It's impossible to tell what without a more thorough example. –  Aug 28 '14 at 22:02

1 Answers1

4

If the output of console.log(response) is

{"type":"success","desc":"something"}

then response is most likely still a string (containing JSON), and strings don't have a type property:

> "foo".type == "error" // `undefined` is never equal to a string
false

Objects usually look differently in the console:

> console.log({"type":"success","desc":"something"})
Object {type: "success", desc: "something"} // in Chrome and Firefox at least

Solution: Parse the string first:

response = JSON.parse(response);

Related to jQuery:

I noticed that you intend to let jQuery parse the JSON for you, but you are passing "json" to the wrong function. You have to pass it to $.post(...), not to .done(...):

$.post("sendEmail.php", post_data, "json").done(...);
// instead of 
// $.post("sendEmail.php", post_data).done(..., "json");

Then you don't need to parse it manually.


Related: Parse JSON in JavaScript?

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • I tried manually parsing the JSON but it didn't change anything, moving the ,"json" to the .post method didnt do anything too. I console.log the response right before the if statement then I console.log( response.type ) // "error" and then I console.log( response.type != "error" ) // true – Иван Божков Aug 28 '14 at 22:19
  • What does `console.log( response.type.length )` output? – Felix Kling Aug 28 '14 at 22:20
  • It outputs "Can't find property length of undefined". I wrote just JSON.parse(response) without assigning it to anything that was the problem. Thanks! – Иван Божков Aug 28 '14 at 22:28