admin管理员组文章数量:1287969
I'm using $.post method to make ajax calls.
I have a script (php) that checks for user existing in database and returns (echo's) 1 if exist and 0 if not.
Is it possible to return true and false so javascript recognize it as boolean ?
I'm using $.post method to make ajax calls.
I have a script (php) that checks for user existing in database and returns (echo's) 1 if exist and 0 if not.
Is it possible to return true and false so javascript recognize it as boolean ?
- 1 Why do you need it? it is quite easy to check strings in js – Igor Commented Jan 7, 2011 at 14:40
-
One reason for wanting to deal with Boolean
true
/false
in JavaScript as well as PHP can be to maintain conceptual unity. I won't have to think that it's PHP so usetrue
, or it's JavaScript so use1
and also no conversion required when to and fro. – user216084 Commented Jun 9, 2014 at 5:43
4 Answers
Reset to default 3No, the values will always be returned as text. You need to pare the values in your JavaScript.
if (data == '1') {
//it's true
} else {
// it's false
}
Actually the string "0"
is interpreted as false
by JavaScript and "1"
is interpreted as true
, so you can simply work with the string value as a boolean. But then you can't use ===
to pare.
http://api.jquery./jQuery.post/
mentions an option to parse data as JSON, so in theory you could use php to echo data in json form.
As far as I remember, js takes 0 like a false.
So you can use
if(ans) ...; else ...
I'm not sure, but try it!
本文标签: Return truefalse to javascript from php fileStack Overflow
版权声明:本文标题:Return truefalse to javascript from php file - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741272287a2369527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论