首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

jquery的Ajax返回值如何取

2013-03-04 
jquery的Ajax返回值怎么取?!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www

jquery的Ajax返回值怎么取?


<!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了呀?
[解决办法]

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;
}
}
[解决办法]
回调函数中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;//////
        }
    }

热点排行