Ajax使用基础示例

Get请求

 

functionloadXMLDoc()

{

var xmlhttp;

if(window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera,Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()//会被触发4次

  {

  if (xmlhttp.readyState==4&& xmlhttp.status==200)//响应已就绪

    {

   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("GET","/ajax/demo_get.asp",true);

xmlhttp.send();

}

Post请求

 

functionloadXMLDoc()

{

var xmlhttp;

if(window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera,Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 &&xmlhttp.status==200)

    {

   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

  }

xmlhttp.open("POST","/ajax/demo_post2.asp",true);

xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//区别之处

xmlhttp.send("fname=Bill&lname=Gates");//区别之处

}

非异步请求

functionloadXMLDoc()

{

var xmlhttp;

if(window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera,Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.open("GET","/ajax/test1.txt",false);

xmlhttp.send();

document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

}

</script>

responseXML响应方式

 

functionloadXMLDoc()

{

var xmlhttp;

var txt,x,i;

if(window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera,Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 &&xmlhttp.status==200)

    {

    xmlDoc=xmlhttp.responseXML;

    txt="";

   x=xmlDoc.getElementsByTagName("title");

    for (i=0;i<x.length;i++)

      {

      txt=txt + x[i].childNodes[0].nodeValue +"<br />";

      }

   document.getElementById("myDiv").innerHTML=txt;

    }

  }

xmlhttp.open("GET","/example/xmle/books.xml",true);

xmlhttp.send();

}

callBack函数

 

functionloadXMLDoc(url,cfunc)

{

if(window.XMLHttpRequest)

  {// code for IE7+, Firefox, Chrome, Opera,Safari

  xmlhttp=new XMLHttpRequest();

  }

else

  {// code for IE6, IE5

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

xmlhttp.onreadystatechange=cfunc;

xmlhttp.open("GET",url,true);

xmlhttp.send();

}

functionmyFunction()

{

loadXMLDoc("/ajax/test1.txt",function()

  {

  if (xmlhttp.readyState==4 &&xmlhttp.status==200)

    {

   document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    }

  });

}

请求XML 文件

 

function loadXMLDoc(url)
{
var xmlhttp;
var txt,xx,x,i;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=newXMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
 }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 &&xmlhttp.status==200)
    {
    txt="<tableborder='1'><tr><th>Title</th><th>Artist</th></tr>";
   x=xmlhttp.responseXML.documentElement.getElementsByTagName("CD");
    for (i=0;i<x.length;i++)
      {
      txt=txt +"<tr>";
     xx=x[i].getElementsByTagName("TITLE");
        {
        try
          {
          txt=txt +"<td>" + xx[0].firstChild.nodeValue +"</td>";
          }
        catch (er)
          {
          txt=txt + "<td></td>";
          }
        }
    xx=x[i].getElementsByTagName("ARTIST");
      {
        try
          {
          txt=txt +"<td>" + xx[0].firstChild.nodeValue +"</td>";
          }
        catch (er)
          {
          txt=txt + "<td></td>";
          }
        }
      txt=txt +"</tr>";
      }
    txt=txt +"</table>";
   document.getElementById('txtCDInfo').innerHTML=txt;
    }
 }
xmlhttp.open("GET",url,true);
xmlhttp.send();
}

进行一次head请求

 

var xmlhttp;

functionloadXMLDoc(url)

{

xmlhttp=null;

if(window.XMLHttpRequest)

  {// code for Firefox, Mozilla, IE7, etc.

  xmlhttp=new XMLHttpRequest();

  }

else if(window.ActiveXObject)

  {// code for IE6, IE5

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

if (xmlhttp!=null)

  {

  xmlhttp.onreadystatechange=state_Change;

  xmlhttp.open("GET",url,true);

  xmlhttp.send(null);

  }

else

  {

  alert("Your browser does not supportXMLHTTP.");

  }

}

 

functionstate_Change()

{

if(xmlhttp.readyState==4)

  {// 4 = "loaded"

  if (xmlhttp.status==200)

    {// 200 = "OK"

   document.getElementById('p1').innerHTML=xmlhttp.getAllResponseHeaders();

    }

  else

    {

    alert("Problem retrieving data:"+ xmlhttp.statusText);

    }

  }

}

进行一次指定的head请求

 

var xmlhttp;

functionloadXMLDoc(url)

{

xmlhttp=null;

if(window.XMLHttpRequest)

  {// all modern browsers

  xmlhttp=new XMLHttpRequest();

  }

else if(window.ActiveXObject)

  {// for IE5, IE6

  xmlhttp=newActiveXObject("Microsoft.XMLHTTP");

  }

if (xmlhttp!=null)

  {

  xmlhttp.onreadystatechange=state_Change;

  xmlhttp.open("GET",url,true);

  xmlhttp.send(null);

  }

else

  {

  alert("Your browser does not supportXMLHTTP.");

  }

}

 

functionstate_Change()

{

if(xmlhttp.readyState==4)

  {// 4 = "loaded"

  if (xmlhttp.status==200)

    {// 200 = "OK"

   document.getElementById('p1').innerHTML="This file was lastmodified on: " + xmlhttp.getResponseHeader('Last-Modified');

    }

  else

    {

    alert("Problem retrieving data:"+ xmlhttp.statusText);

    }

  }

}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值