我有以下编码,
<script type="text/javascript">
$(document).ready(function(){
(function ajaxInterval() {
$.ajax({
url:'clmcontrol_livematchupdate',
type:'post',
dataType: 'json',
success: function(data) {
$('#lblbattingteam').html(data.battingnow);
$('#lblscore').html(data.score);
$('#lblwickets').html(data.wickets);
$('#lblovers').html(data.overs);
$('#lblballs').html(data.balls);
$('#lblextras').html(data.extras);
$('#lblrr').html(data.runrate);
$('#lblbowlingteam').html(data.bowlingnow);
$('#lblbowler').html(data.currentbowler);
$('#lblbowlerovers').html(data.bowlerovers);
$('#lblbowlerballs').html(data.bowlerballs);
$('#lblrunsgiven').html(data.runsgiven);
$('#lblextrasgiven').html(data.extrasgiven);
$('#lblwicketstaken').html(data.wicketstaken);
$('#lblecon').html(data.econ);
},
complete: function() {
// Schedule the next request when the current one has been completed
setTimeout(ajaxInterval, 4000);
}
});
})();
});
</script>
一开始这个工作正常,但经过几次尝试后这不起作用,标签也没有更新,我也没有对servlet进行任何更改。我的servlet如下所示,请帮我解决这个问题。
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
response.setContentType("text/json");
PrintWriter out = response.getWriter();
try {
JSONObject json = new JSONObject();
ResultSet rscurteaminfo = clmmodel_database.selectQuery("select ct.teamname, cm.score, cm.wickets, cm.extras, cm.balls, cm.currentbatsman, cm.currentbowler from clm_match cm inner join clm_team ct on ct.teamid = cm.battingnow where cm.matchid = '" + session.getAttribute("matchid") + "'");
ResultSet rsbowlingteaminfo = clmmodel_database.selectQuery("select ct.teamname, cm.currentbowler ,cp.playerfname, cpm.ballsbowled, cpm.wickets, cpm.runsgiven, cpm.extrasgiven from clm_match cm inner join clm_team ct on ct.teamid = cm.bowlingnow inner join clm_playerinmatch cpm on cpm.playerid = cm.currentbowler inner join clm_player cp on cp.playerid = cm.currentbowler where cm.matchid = '" + session.getAttribute("matchid") + "'");
ResultSet rscurbatsmen = clmmodel_database.selectQuery("select cp.playerid, cp.playerfname , cpm.runsscored, cm.currentbatsman from clm_playerinmatch cpm inner join clm_player cp on cp.playerid = cpm.playerid inner join clm_match cm on cm.matchid = cpm.matchid where cm.battingnow = cp.teamid and cpm.currentstate = 'Batting' and cm.matchid = '" + session.getAttribute("matchid") + "'");
while (rscurteaminfo.next()) {
//Calculate overs and balls
int balls = (Integer) rscurteaminfo.getInt("balls");
int ballsperover = 6;
int k = balls % ballsperover;
int overs = 0;
if (balls >= 6) {
overs = (balls - k) / ballsperover;
}
//Calculate overs and balls
double runs = Double.parseDouble(rscurteaminfo.getString("score"));
double ballshalf = k / 6.0;
double totalovers = overs + ballshalf;
double runrate = runs / totalovers;
double inf = Double.POSITIVE_INFINITY;
if (runrate == inf) {
runrate = 0.0;
}
json.put("battingnow", rscurteaminfo.getString("teamname"));
json.put("score", rscurteaminfo.getString("score"));
json.put("wickets", rscurteaminfo.getString("wickets"));
json.put("overs", overs);
json.put("balls", k);
json.put("extras", rscurteaminfo.getString("extras"));
json.put("runrate", runrate);
}
while (rsbowlingteaminfo.first()) {
int balls = (Integer) rsbowlingteaminfo.getInt("ballsbowled");
int ballsperover = 6;
int k = balls % ballsperover;
int overs = 0;
if (balls >= 6) {
overs = (balls - k) / ballsperover;
}
double runs = Double.parseDouble(rsbowlingteaminfo.getString("runsgiven"));
double ballshalf = k / 6.0;
double totalovers = overs + ballshalf;
double econ = 0.0;
econ = runs / totalovers;
double inf = Double.POSITIVE_INFINITY;
if (econ == inf) {
econ = 0.0;
}
json.put("bowlingnow", rsbowlingteaminfo.getString("teamname"));
json.put("currentbowler", rsbowlingteaminfo.getString("playerfname"));
json.put("bowlerovers", overs);
json.put("bowlerballs", k);
json.put("runsgiven", rsbowlingteaminfo.getString("runsgiven"));
json.put("extrasgiven", rsbowlingteaminfo.getString("extrasgiven"));
json.put("wicketstaken", rsbowlingteaminfo.getString("wickets"));
json.put("econ", econ);
}
out.print(json);
} catch (Exception e) {
e.printStackTrace();
}
}