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);}完美解决。
在jQuery AJAX调用后台验证时,接收到的Boolean结果被转换为字符串。通过Firebug发现,直接判断不会执行。尝试用Boolean()转换和underscore的isBoolean方法,但效果不一。需进一步解决将字符串转回Boolean类型的问题。

893

被折叠的 条评论
为什么被折叠?



