AJAX

来源:互联网 发布:linux elf文件 破解 编辑:程序博客网 时间:2024/05/16 07:28

创建XMLHttpRequest对象

XMLHttpRequest 用于在后台与服务器交换数据。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。

var xmlhttp;if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }

向服务器发送请求

xmlhttp.open("GET","test1.txt",true);xmlhttp.send();
open(method,url,async);//true异步,false同步//规定请求的类型、URL以及是否异步处理请求
send(string)//将请求发送给服务器,string仅用于POST请求

GET请求

一个简单的请求

<html><head><script type="text/javascript">function loadXMLDoc(){var xmlhttp;if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","/ajax/demo_get.asp",true);xmlhttp.send();}</script></head><body><h2>AJAX</h2><button type="button" onclick="loadXMLDoc()">请求数据</button><div id="myDiv"></div></body></html>

在上面的例子中可能得到的是缓存的数据,添加唯一的ID可以解决这个问题

xmlhttp.open("GET","demo_get.asp?t=" + Math.random(),true);xmlhttp.send();

通过GET方法发送数据时,需要向URL中添加信息

xmlhttp.open("GET","demo_get2.asp?fname=Bill&lname=Gates",true);xmlhttp.send();

结果如下:
这里写图片描述

POST请求

一个简单的POST请求

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

如果希望向HTML表单那样POST数据,请使用setRequestHeader()来添加HTTP头。然后在send()方法中规定你希望发送的数据:

xmlhttp.open("POST","ajax_test.asp",true);xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");xmlhttp.send("fname=Bill&lname=Gates");

URL-服务器上的文件

因为AJAX指的是异步javasc和XML,XMLHttpRequest对象如果要是用AJAX的话,open()中的async必须设置为true

Async = true

当使用 async=true 时,请规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数:

xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","test1.txt",true);xmlhttp.send();

服务器响应

responseText//获取字符串形式的响应responseXML//获取XML形式的响应
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
xmlDoc=xmlhttp.responseXML;txt="";x=xmlDoc.getElementsByTagName("ARTIST");for (i=0;i<x.length;i++)  {  txt=txt + x[i].childNodes[0].nodeValue + "<br />";  }document.getElementById("myDiv").innerHTML=txt;

onreadystatechange事件

当请求被发到服务器时,我们需要执行一些基于响应的任务。
每当readyState改变时,就会触发onreadystatechange事件
readyState属性存有XMLHttpRequest的状态信息
下面是XMLHttpRequest对象的三个重要属性

1、 onreadystatechange 存储函数或函数名,每当readyState属性改变时,就会调用该函数。
2、 readyState

 0:请求未初始化 1:服务器连接以建立 2:请求已接收 3:请求处理中 4:请求已完成且响应已就绪

3、status

   200:OK   400:未找到页面

使用Callback函数

callback函数是一种以参数形式传递给另一个函数的函数
如果您的网站上存在多个 AJAX 任务,那么您应该为创建 XMLHttpRequest 对象编写一个标准的函数,并为每个 AJAX 任务调用该函数。
该函数调用应该包含 URL 以及发生 onreadystatechange 事件时执行的任务(每次调用可能不尽相同):

function myFunction(){    loadXMLDoc("url",function(){        if(xmlhttp.readyState==4&&xmlhttp.status==200)        {             document.getElementById("myDiv").innerHTML=xmlhttp.responseText;        }    })}

ASP/PHP请求实例

<html><head><script type="text/javascript">function showHint(str){var xmlhttp;if (str.length==0)  {   document.getElementById("txtHint").innerHTML="";  return;  }if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","/ajax/gethint.asp?q="+str,true);xmlhttp.send();}</script></head><body>    <h3>请在下面的输入框中键入字母(A - Z):</h3>    <form action="">     姓氏:<input type="text" id="txt1" onkeyup="showHint(this.value)" />//由onkeyup事件触发showHint()    </form>    <p>建议:<span id="txtHint"></span></p> </body></html>

如果输入框为空 (str.length==0),则该函数清空 txtHint 占位符的内容,并退出函数。

如果输入框不为空,showHint() 函数执行以下任务:
创建 XMLHttpRequest 对象
当服务器响应就绪时执行函数
把请求发送到服务器上的文件
请注意我们向 URL 添加了一个参数 q (带有输入框的内容)

AJAX用来与数据库进行动态通信

<html><head><script type="text/javascript">function showCustomer(str){var xmlhttp;    if (str=="")  {  document.getElementById("txtHint").innerHTML="";  return;  }if (window.XMLHttpRequest)  {// code for IE7+, Firefox, Chrome, Opera, Safari  xmlhttp=new XMLHttpRequest();  }else  {// code for IE6, IE5  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");  }xmlhttp.onreadystatechange=function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;    }  }xmlhttp.open("GET","/ajax/getcustomer.asp?q="+str,true);xmlhttp.send();}</script></head><body><form action="" style="margin-top:15px;"> <label>请选择一位客户:<select name="customers" onchange="showCustomer(this.value)" style="font-family:Verdana, Arial, Helvetica, sans-serif;"><option value="APPLE">Apple Computer, Inc.</option><option value="BAIDU ">BAIDU, Inc</option><option value="Canon">Canon USA, Inc.</option><option value="Google">Google, Inc.</option><option value="Nokia">Nokia Corporation</option><option value="SONY">Sony Corporation of America</option></select></label></form><br /><div id="txtHint">客户信息将在此处列出 ...</div></body></html>

检查是否已选择某个客户
创建 XMLHttpRequest 对象
当服务器响应就绪时执行所创建的函数
把请求发送到服务器上的文件
请注意我们向 URL 添加了一个参数 q (带有输入域中的内容)

原创粉丝点击