javascript strange comparison of strings
This question already has an answer here:
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?
链接地址: http://www.djcxy.com/p/19436.html上一篇: typeof方法在Javascript中如何工作?
下一篇: javascript奇怪的比较字符串