<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.8.0.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        var test = {
            getValue: function (key) {
                if (key == '1') {
                    return 'A';
                } else {
                    return 'B';
                }
            },
            getAjaxValue: function (key) {
                $.ajax({
                    type: "post", async: false, cache: false, dataType: "text",
                    url: '../../Handler/JavaScriptSession.ashx',
                    data: [
                    { name: "action", value: "read" },
                    { name: "name", value: key },
                    { name: "value", value: "" }
                    ],
                    success: function (result) {
                        if (result != "error" || result != "true") {
                            alert("内部"+result);//这值能得到,并成功alert出
                            return "返回值"+result;
                        }
                    },
                    error: function () {
                        alert("错误");
                    }
                });
            }
        }
    </script>
</head>
<body>
<div >
    <a onclick="alert(test.getValue('1'));">测试1</a>
    <br />
    <a onclick="alert(test.getAjaxValue('1'));">测试2</a>
</div>
</body>
</html>为什么,测试1 我能得到return的值,测试2我确无法得到?Ajax的async我已经设置为false了呀?

解决方案 »

  1.   

     data: [
                        { name: "action", value: "read" },
                        { name: "name", value: key },
                        { name: "value", value: "" }
                        ],data这样传不行吧
    data (Object,String) : 发送到服务器的数据。将自动转换为请求字符串格式。GET 请求中将附加在 URL 后。查看 processData 选项说明以禁止此自动转换。必须为 Key/Value 格式。如果为数组,jQuery 将自动为不同值对应同一个名称。如 {foo:["bar1", "bar2"]} 转换为 '&foo=bar1&foo=bar2'。
      

  2.   

    没问题的,KeyValue只是举例方便这样写的。实际不是这样用的。
    跟踪发现ajax成功执行。success中得到了result的值。
     alert("内部"+result);也显示了。
    但test.getAjaxValue('1')得不到return的值。
      

  3.   


    var test = {
    getValue : function(key) {
    if (key == '1') {
    return 'A';
    } else {
    return 'B';
    }
    },
    getAjaxValue : function(key) {
    var _result;
    $.ajax({
    type : "post",
    async : false,
    cache : false,
    dataType : "text",
    url : 'index.php',
    data : {},
    success : function(result) {
    if (result != "error" || result != "true") {
    alert("内部" + result);//这值能得到,并成功alert出
    _result =  "返回值" + result;
    }
    },
    error : function() {
    alert("错误");
    }
    });
    return _result;
    }
    }
      

  4.   

    success里面的return 是回调函数的返回值,不是getAjaxValue 的返回值。
      

  5.   

    回调函数中return 没什么意义,而且test.getAjaxValue这个方法没有返回值。改成下面的    var test = {
            getValue: function (key) {
                if (key == '1') {
                    return 'A';
                } else {
                    return 'B';
                }
            },
            getAjaxValue: function (key) {
                var s = false; ////////
                $.ajax({
                    type: "post", async: false, cache: false, dataType: "text",
                    url: '../../Handler/JavaScriptSession.ashx',
                    data: [
                        { name: "action", value: "read" },
                        { name: "name", value: key },
                        { name: "value", value: "" }
                        ],
                    success: function (result) {
                        if (result != "error" || result != "true") {
                            alert("内部" + result); //这值能得到,并成功alert出
                            s = "返回值" + result; ///////////
                        }
                    },
                    error: function () {
                        alert("错误");
                    }
                });
                return s;//////
            }
        }