2011-08-25 69 views
0

我想要一個從服務器獲取數據的進度條,所以我創建了兩個servlet,第一個(process)啓動該進程,並在結束時返回結果;第二個(GetEvent)每500毫秒從會話中獲取進度信息。未執行JQuery回調?

所有這些都正常工作,並且進度信息顯示正確,但 處理Servlet的回調從不執行。

<html> 
    <head> 
     <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
     <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 

     <script> 
     $(document).ready(function() { 
     process(); 
    $("#progressbar").progressbar({ value: 0 }); 
     $("#progressStatus").html(""); 

     getEvent(); 
     }); 
     function process() 
     { 
     $.getJSON("process", function(result){ 
     //never executed 
     alert("Result: "); 

     }); 
     } 
     function getEvent() 
     { 
     $.getJSON("GetProgressEvent", function(data) {  
     $.each(data.ProgressEvents, function(){ 
     $("#progressbar").progressbar({ value: this.progress }); 
     $("#progressStatus").html(this.status); 
     }); 
     }); 
      setTimeout(getEvent, 500); 
     } 
     </script> 
    </head> 
    <body style="font-size:62.5%;"> 

    <div id="progressbar"></div> 
    <div id ="progressStatus"></div> 
    </body> 
    </html> 

我剛開始使用JQuery,我不知道這段代碼有什麼問題。

+0

你確定GetProgressEvent調用實際上返回?您是否使用Web瀏覽器檢查器(例如Chrome或FireFox中的Net面板)檢查了HTTP通信? –

回答

2

你打電話到$ .getJSON與url「process」 這個函數沒有錯誤處理,如果響應有問題或無效JSON返回那麼回調將不會被調用。

http://api.jquery.com/jQuery.getJSON/

jQuery.getJSON(URL,[數據],[成功(數據,textStatus,jqXHR)])

網址的含有URL字符串到的請求被髮送。

數據與請求一起發送到服務器的映射或字符串。

success(data,textStatus,jqXHR)如果請求成功,則執行的回調函數。

嘗試在地方「過程」的加入有效的URL,如果失敗使用 $就方法如下

$.ajax({ 
    url: "mydomain.com/url", 
    type: "POST", 
    dataType: "json", 
    data: $.param($("Element or Expression")), 

    complete: function() { 
    //called when complete 
    }, 

    success: function() { 
    //called when successful 
}, 

    error: function() { 
    //called when there is an error 
    }, 
}); 
+0

確切地說,從servlet返回的JSON無效:( – user405458