2011-02-15 140 views
1

我有一個問題,我的JavaScript不等待電話回覆。我現在已經是javascript了,所以我想知道如何讓這個方法調用等待結果。自用戶上傳後,我無法控制前兩個snipper。我可以使用jQuery或純JavaScript。提前致謝!JavaScript在等待返回前繼續

我有這個JavaScript調用

var value = somemethod("cmi.location"); 
//This is not getting set since it does not wait. alerts 'undefined' 
alert(value); 

和的someMethod看起來像下面的代碼,

function somemethod(element){ 
var result; 
result = API1.GetValue(element); 
return result; 
} 

API是通過做下面的代碼實例化一個窗口對象。從這一點我可以訪問代碼片段。

var API1 = new API(); 

API是在JavaScript對象,看起來像這樣:

function API(){ 

}; 
API.prototype.GetValue=API_GetValue; 

function API_GetValue(parameter){ 
$.ajax({ 
    type:"POST", 
    async:false, 
    url:"method.do", 
    dataType:"xml", 
    data: {action: 'getValue', parameter: parameter, value: ''}, 
    success:function(data){ 
    //I am getting 0 here 
     return $(data).find('search').text(); 
    } 
}); 
} 

回答

3
function API_GetValue(parameter){ 
var newdata; 
$.ajax({ 
    type:"POST", 
    async:false, 
    url:"method.do", 
    dataType:"xml", 
    data: {action: 'getValue', parameter: parameter, value: ''}, 
    success:function(data){ 
    //I am getting 0 here 
     newdata = $(data).find('search').text(); 
    } 
}); 
return newdata; 
} 

你也可以做這樣的事情:

function API_GetValue(parameter){ 
var newdata = $.ajax({ 
    type:"POST", 
    async:false, 
    url:"method.do", 
    dataType:"xml", 
    data: {action: 'getValue', parameter: parameter, value: ''} 
}).responseText; 
return $(newdata).find('search').text(); 
} 
+0

我已經嘗試過這一點,但它仍然沒有不行。我會再試一次,你能告訴我爲什麼你認爲這會起作用嗎? – mezzie 2011-02-15 14:09:11

相關問題