标签:javascript js boolean ajax json
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
再想其他方法:先用字符串比较然后在判断

<pre code_snippet_id="537953" snippet_file_name="blog_20141201_4_5931945" name="code" class="javascript">result = $.parseJSON(result);
if(!result)
{console.log(result);
}
标签:javascript js boolean ajax json
原文地址:http://blog.csdn.net/zml6308/article/details/41643399