<script>
//when page is ready do the following
$(document).ready(function()
{
//set interval of refresh
setInterval(doAjaxMethod, 1000);
});
function doAjaxMethod(id)
{
$.ajax({
url: "getStatus/"+id,
dataType: "json",
success: function(json)
{
$('#ajaxStatus').html(json.status);
}
});
</script>
<%
//How can I do something like this
int n = object.size();
for(int i=0; i<n; i++)
{
doAjaxMethod(object.getId());
}
%>
<div id=ajaxStatus> status updates here </div>
最佳答案
我不会在 scriplet 中混合调用 javascript。相反,在 javascript 中管理循环。如果您描述更多您想要做的事情,我可以更具体。看来您在 jsp 上有一个可用的容器对象,并且您需要循环以获取容器中内容的状态。与其将该对象传递给 jsp,为什么不创建一个端点,jsp 上的 javascript 可以在 ajax 中调用该端点来获取您需要的所有数据。
关于javascript - 我如何循环ajax请求(使用jquery)和jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6469547/