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?