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。
评论
发表评论

您还没有登录,请登录后发表评论

gaoxing_china
搜索本博客
博客分类
最近加入圈子
存档
最新评论