I am working on an ajax function that is responsible for sending emails and receiving responses from the server in a JSON format with type being 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")
});
What I find intriguing is when I console.log(response)
, it shows something like
{"type":"success","desc":"something"}
. However, immediately after that, console.log( (response.type == "error") ) // TRUE
.
If I store the console logged response into a variable, say
a = {"type":"success","desc":"something"}
, then a.type == "error"
evaluates to false.
Could someone shed some light on this peculiar behavior?