javascript后台直接返回Boolean类型处理
时间:2014-12-01 10:07:42
收藏:0
阅读:318
r最近使用jqueyr的ajax后台验证,直接返回一个Boolean类型的值的到前台,使用json格式传到前台
var result = $.ajax({ url: '/'+window.location['pathname'].split('/')[1]+'/resourcePrivate/validateResourcePrivate?rid='+rid, async: false, dataType: "json" }).responseText;
console.log(result);结果是false。
在代码中直接使用
if(!result){ console.log(result); }
一直没有输出。

使用firebug调试后发现竟然是字符串,所以下面的判断无论如何都不会执行。
好吧,到这里只要把result转换成Boolean类型就可以吧,想想很简单!
1.使用Boolean(result); 结果是true
2.使用underscore的isBoolean,结果是false
再想其他方法:先用字符串比较然后在判断
result = result=="false"?false:true;
暂时解决,还有没有其他方法呢?
最后想到,这个里面使用的是json,那就用jquery的方法试试

<pre code_snippet_id="537953" snippet_file_name="blog_20141201_4_5931945" name="code" class="javascript">result = $.parseJSON(result);
if(!result)
{
console.log(result);
}
完美解决。
原文:http://blog.csdn.net/zml6308/article/details/41643399
评论(0)