日期:2014-05-17  浏览次数:20461 次

一组JS返回值为什么不是布尔类型


        function test() {
            var b;
                $.get('Ajax.aspx', function (data) {

                    if ($.trim(data) != "") {
                        alert(data + "z");
                        b= true;
                    }
                    else {
                        alert(data + "j");
                        b= false;
                    }
                    alert(b);//此处结果是 true;
                });
                alert(b);//此处结果是 unfinished
                return b;
        }


为什么返回的是unfinished的?哪里错了?
------解决方案--------------------
首先要理解ajax是个异步的东西
function test() {
            var b;
                $.get('Ajax.aspx', function (data) {
 
                    if ($.trim(data) != "") {
                        alert(data + "z");
                        b= true;
                    }
                    else {
                        alert(data + "j");
                        b= false;
                    }