【发布时间】:2014-10-22 21:34:31
【问题描述】:
我有一个 ajax 函数,它可以向某处发送电子邮件并从服务器接收来自类型 = 成功或错误的 json 对象的响应
$("#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")
});
有趣的是,如果我 console.log(response) 我得到例如 {"type":"success","desc":"something"} 然后紧接着 console.log( (response.type == "error") ) // TRUE
如果我从响应中获取控制台日志并将其分配给一个变量,例如 a = {"type":"success","desc":"something"},则 a.type == "error" 为 false。
谁能解释一下?
【问题讨论】:
-
我的问题是什么时候你会尝试
console.log(response)吗? -
{"type":"success"...type == "error"嗯...您显然遗漏了一些东西,因为您的示例完全关闭了。如果没有更详尽的示例,就无法判断是什么。
标签: javascript ajax