微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ajax结合js实现服务器

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
#divProgress{width:300px;height:24px;position:relative;}
#divProgress div{position:absolute;left:0;top:0;height:24px;}
#progressBg{background-color:#B9F8F9;z-index:10;}
#progresstext{z-index:15;text-align:center;width:100%;}
</style>
</head>
<body>
<div id="divProgress">
<div id="progressBg"></div>
<div id="progresstext"></div>
</div>
<br />
<button onclick="send()">�ύ���</button>
<script>
var t = document.getElementById("progresstext");
var bg = document.getElementById("progressBg");
function send(){
t.innerHTML = "loading...";
bg.style.width = "0px";
var xhr = new window.XMLHttpRequest();
if(!window.XMLHttpRequest){
try {
xhr = new window.ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
xhr.open("post","index.jsp?count=6");
var oldSize=0;
xhr.onreadystatechange = function(){
if(xhr.readyState > 2){
var tmpText = xhr.responseText.substring(oldSize);
oldSize = xhr.responseText.length;
if(tmpText.length > 0 ){
t.innerHTML = tmpText + "/6";
var width = parseInt(tmpText)/6*300;
bg.style.width = width+"px";
}
}
if(xhr.readyState == 4){

t.innerHTML = "ִ�����";
bg.style.width = "300px";
}
}
xhr.send(null);
}
</script>
</body>
</html>

服务端:

index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><% // 下面设置Content-Type:application/x-javascript 是为了适应Webkit的浏览器(chrome,safari) response.setHeader("Content-Type","application/x-javascript"); int count = 6; // 处理6条数据 for(int i=0;i<count;i++){ // 处理完毕一条,输出结果到客户端 out.println(i+1); out.flush(); // 这里假设每条数据处理时间为1秒 Thread.currentThread().sleep(1000); } %>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐