2011-08-25 70 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