2013-11-20 88 views
0

嗨,我們一直試圖解決這個問題在過去的幾個小時,並決定ID在這裏睡覺之前把它放在這裏,無論如何,問題是我需要確保GET/POST請求在繼續執行代碼之前已經100%處理完畢,我用firefox addon sdk中的定時器解決了這個問題,但是因爲這是java腳本,它鎖定了UI,所以我一直在尋找解決方案我偶然發現了Felix Kling的一個潛在解決方案,「How do I return the response from an asynchronous call?」。儘管我嘗試過沒有成功,所以我想知道如果有人能告訴我什麼是我做錯了,或者如果我甚至不能使用這個解決方案來做我想做的事情。在繼續之前確保異步GET/POST請求已完成

download_status(download_database()); 

function download_status(downloadStatus){ 
    if(downloadStatus==0){ 
    console.log(regexArray.length); 
    } 
    else{ 
    console.log("oh no"); 
    } 
} 

function download_database(){ 
    var downloadDone = 0; 
    var requestDB = request.Request({ 
    url: "http://phyzical.pythonanywhere.com/download_db/", 
    onComplete: function(response){ 
     console.log(response.statusText); 
     if(response.json == null){ 
     console.log("cannot retreive json properly.") 
     } 
     else{ 
     var dbInfoLength = response.json.length; 
     var idNumber = 0; 
     for(var x=0;x<dbInfoLength;x++){ 
      try{ 
      var patt1=new RegExp(response.json[x].regex); 
      idArray[idNumber] = response.json[x].id; 
      regexArray[idNumber] = response.json[x].regex; 
      incorrectMessageArray[idNumber] = response.json[x].incorrect_information; 
      correctMessageArray[idNumber] = response.json[x].correct_information; 
      idNumber++;   
      } 
      catch(e){ 
      console.log("The annotation with the id: \""+ response.json[x].id+" " + e.message + "\" is wrong."); 
      } 
     } 
     downloadDone = 0; 
     } 
    }, 
    }).get(); 
    return downloadDone; 
} 

黯然regexArray.length日誌從一開始的「0」,然後「確定」,然後將捕獲的火災之一,所以我知道的信息被存儲到陣列簡單,同樣的問題我開始仍然存在。

任何幫助,將不勝感激。

+0

您鏈接的帖子 - 非常清楚地解釋它的一切。你需要使用回調。 – webduvet

回答

1

我沒有實現任何你的邏輯,但是我重寫了你的請求代碼(因爲這是你遇到的麻煩),以使用jQuery的AJAX方法,而不是Firefox的請求。

<h1>JSON Data Fetch Test</h1> 
<div id="data"></div> 

<script src="jquery-2.0.3.min.js"></script> 
<script> 
var dataBlock = document.getElementById("data"); 

function GetData() 
{ 
    try 
    { 
     $.ajax({ 
      url: "http://phyzical.pythonanywhere.com/download_db/", 
      crossDomain: true, 
      dataType: 'text', 
      context: document.body, 
      error: reportError 
     }).done(processResponse); 
    } 
    catch(e) 
    { 
     dataBlock.textContent = "Request Error: " + e.message; 
    } 
} 

function reportError() 
{ 
    dataBlock.textContent = "Some kind of problem..."; 
} 

function processResponse(data) 
{ 
    dataBlock.textContent = data; 
    var obj = JSON.parse(data); 
    /*Do all the things you need to do with your data here.*/ 
} 

dataBlock.textContent = "Fetching data..."; 
GetData(); 
</script> 
4

如果您在繼續之前需要回應,您確實有兩種選擇:一種是將響應傳遞給AJAX響應處理程序中的回調。一旦收到響應,回調將成爲應用程序的入口點。

另一種選擇是使用同步XHR請求。然而,這種方法的缺點是你的用戶界面會被鎖定,直到請求完成。

乾杯

+0

這也是一個有效的答案,我最終解決了我的CORS問題,因此可能會發生ajax調用,請參閱millie對我最終使用的答案。 – phyzical

0

我不能在此刻回答太多的代碼,但是當我面對這類問題,我通常最終調用「下一個」功能(在你的情況download_status())裏面的Get的onComplete,無論是硬編碼還是回調。這將確保它在完成後被調用。

+0

這個解決方案唯一的問題是同樣的問題仍然存在,因爲程序的其餘部分需要將數據庫信息做下一行代碼。感謝您的嘗試。 – phyzical

1

Addon SDK的promise module可以讓你按照自己的意願去做任何事情。

相關問題