2013-11-20 89 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可以让你按照自己的意愿去做任何事情。

相关问题