jQuery:ajax调用成功后返回数据我有这样的东西,它是一个简单的调用脚本,给我一个值,一个字符串..function testAjax() {
$.ajax({
url: "getvalue.php",
success: function(data) {
return data;
}
});}但如果我打电话给这样的话var output = testAjax(svar); // output will be undefined...那么我怎么能返回这个值呢?以下代码似乎也不起作用......function testAjax() {
$.ajax({
url: "getvalue.php",
success: function(data) {
}
});
return data; }
4 回答
慕后森
TA贡献1802条经验 获得超5个赞
从函数返回数据的唯一方法是进行同步调用而不是异步调用,但这会在等待响应时冻结浏览器。
您可以传入一个处理结果的回调函数:
function testAjax(handleData) {
$.ajax({
url:"getvalue.php",
success:function(data) {
handleData(data);
}
});}像这样称呼它:
testAjax(function(output){
// here you use the output});// Note: the call won't wait for the result,// so it will continue with the code here while waiting.
互换的青春
TA贡献1797条经验 获得超6个赞
您可以将async选项添加到false 并返回到ajax调用之外。
function testAjax() {
var result="";
$.ajax({
url:"getvalue.php",
async: false,
success:function(data) {
result = data;
}
});
return result;}添加回答
举报
0/150
提交
取消
