我正在尝试从方法(pollServiceForInfo)返回一个 JSON 对象,但是当我在方法完成后提醒它时,它似乎“丢失”了。我知道这是一个范围界定问题,但是我对如何继续感到困惑。洞察力将不胜感激。
var id=null;
var jsonData = JSON.stringify( {searchRequest:{coordinates: "1,2,3 1,2,3 1,2,3 1,2,3 1,2,3"}} );
$.post("rest/search",jsonData, function(json){
id = json.searchResponse.id;
})
.error(function(jqXHR, textStatus, errorThrown){
alert("obj.responseText: "+jqXHR.responseText + " textStatus: "+textStatus+" errorThrown: "+errorThrown);
})
.success(function(data, status, obj){
// process initial request
var json = pollServiceForInfo(id); // method below
alert(json); // says undefined
});
var pollServiceForInfo = function(id){
//alert('id in pollServiceForInfo '+id);
var jsonResults;
$.get("rest/poll/"+id,function(data){
jsonResults = data.pollResponse;
}).error(function(){
alert('returning error');
return "error";
}).success(function(){
alert('returning data '+jsonResults);
return jsonResults; // is lost after it's returned
});
};