2010-09-03 61 views
0

如果我有以下ajax调用(函数内部),我该如何去返回'test'变量,在这种情况下是一个用于测试目的的字符串?目前我得到的测试没有定义。在ajax调用中返回一个变量

function getMaps(){ 

    mapID = "us"; 

    $.ajax({ 
     type: "GET", 
     url: "getMap.asp", 
     data: "mapID=" + mapID, 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     async: false, 
     success: function(response) { 

      var test = 'text string';  
     } 


     }); 
     return test; 


}; 

回答

1

功能getMaps(){

mapID = "us"; 
var test=""; 
$.ajax({ 
    type: "GET", 
    url: "getMap.asp", 
    data: "mapID=" + mapID, 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    async: false, 
    success: function(response) { 

     test = 'text string';  
    } 


    }); 
    return test; 

};

0

尝试打开弹出框或设置某些文本字段的值。你定义的响应函数将被异步调用。

此外,“测试”没有在返回函数的范围内定义。将定义移动到该函数的顶部 - 它不应该是未定义的,但它仍不能解决问题。

0

vartest前右意味着该变量将是可见的匿名函数内部