2008-02-29
我最常用的Ajax请求代码
关键字: ajax
<script>
//创建xmlhttp请求
function createXMLHttp() {
var xmlHttp = false;
try{
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlHttp = false;
}
}
if(!xmlHttp && typeof XMLHttpRequest!='undefined'){
xmlHttp = new XMLHttpRequest();
}
return xmlHttp;
}
//发送xmlhttp请求,并返回时间串
function callServer1() {
var xmlHttp = createXMLHttp();
var url = "../xml/age_pie.xml";//请求的地址,不可以是跨域的,域名不可以不相同
//alert(url);
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange = function(){
if(xmlHttp.readyState == 4 && xmlHttp.status == 200){
var xmlDoc = xmlHttp.responseXML.documentElement;//以XML对象的形式返回
//var str = xmHttp.responseText;//以文本的形式返回
var node = xmlDoc.getElementsByTagName('data');
var str = node[0].firstChild.nodeValue;
if(str.search(",")!=-1){
}
//document.getElementById('playcount').innerHTML = palystarts[2];
}
}
xmlHttp.send(null);
}
</script>
注意ajax不能跨域请求,会有访问权限问题,解决跨域问题,可以使用中间层,如先在jsp中使用java的URLConnection实现请求,而ajax请求此同服务器的jsp。
发表评论
- 浏览: 6815 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
实时曲线在Web下的实现
如果动态刷新对性能要求比较高,比如一秒刷新一次,那么,用AJax就不行了,本来A ...
-- by jan-zhou -
ibatis连接Mysql数据库
防火墙?
-- by withoutmewang -
ibatis连接Mysql数据库
ibatis + win mysql 5.0 ,没遇到这个问题。 挂几天不用 ...
-- by totobacoo -
ibatis连接Mysql数据库
我记得好像mysql上面有个配置。如果多长时间没有访问数据库。数据库就会主动把连 ...
-- by sillycat -
ibatis连接Mysql数据库
很郁闷,此问题困扰我很久了不知道怎么解决。
-- by gaoxing_china






评论排行榜