ajax调用简单实例
本文实例讲述了ajax调用的方法。分享给大家供大家参考,具体如下:
把url返回的数据,填充到指定id的div中
客户端:test.html
<script>
functioncreateXMLHttp()
{
varret=null;
try{
ret=newActiveXObject('Msxml2.XMLHTTP');
}
catch(e){
try{
ret=newActiveXObject('Microsoft.XMLHTTP');
}
catch(ee){
ret=null;
}
}
if(!ret&&typeofXMLHttpRequest!='undefined')
ret=newXMLHttpRequest();
returnret;
}
functionloadXmlToElement(fragment_url,element_id)
{
varelement=document.getElementById(element_id);
element.innerHTML='<imgsrc="loading.gif"border="0"/>数据加载中...<br/>';
varxmlhttp=createXMLHttp();
xmlhttp.open("GET",fragment_url);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
//alert(xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue);
element.innerHTML=
unescape(xmlhttp.responseXML.getElementsByTagName("data")[0].childNodes[0].nodeValue);
if(!element.innerHTML||element.innerHTML=="/n")
{
try{
element.innerHTML=unescape(xmlhttp.responseXML.firstChild.childNodes[1].nodeValue);
}catch(e){}
}
}
}
xmlhttp.send(null);
}
</script>
<divid="data">
</div>
<script>
varurl="t.xml";
//alert(url);
loadXmlToElement(url,"data");
</script>
把url返回的数据,填充到指定id的div中
服务端:
test.xml
<?xmlversion='1.0'encoding='utf-8'?> <data> <![CDATA[ thisisatest ]]> </data>
注意服务器返回CDATA格式数据,内部可以是任何html代码
希望本文所述对大家ajax程序设计有所帮助。
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短