gpt4 book ai didi

javascript - JQuery 回调未执行?

转载 作者:行者123 更新时间:2023-12-02 20:08:34 24 4
gpt4 key购买 nike

我想要一个从服务器获取数据的进度条,所以我创建了 2 个 servlet,第一个 (process) 启动进程,并在结束时返回结果;第二个 (GetEvent) 每 500 毫秒从 session 获取进度信息..

所有这些工作正常并且进度信息显示正确,但是process 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,我不知道这段代码有什么问题。

最佳答案

您正在使用 url“process”调用 $.getJSON该函数没有错误处理,如果响应有问题或返回无效的 JSON,则不会调用回调。

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

jQuery.getJSON( url, [数据], [成功(数据, textStatus, jqXHR)] )

url 包含请求发送到的 URL 的字符串。

data 随请求发送到服务器的映射或字符串。

success(data, textStatus, jqXHR)请求成功时执行的回调函数。

尝试添加一个有效的 url 来代替“process”,如果失败,请使用$.ajax方法如下

$.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
},
});

关于javascript - JQuery 回调未执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7193194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com