Ajax的简单使用

来源:互联网 发布:长庚医院网络挂号查询 编辑:程序博客网 时间:2024/06/05 01:56
首先,什么是AJAX:
Ajax = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。


Ajax是一种用于快速创建动态网页的技术。
通过在后台与服务器进行少量的数据交换,Ajax可以使网页实现异步更新。也就是说在不对整个网页进行重新加载的情况下,对网页的部分进行更新。


通过XMLHttpRequest对象来和服务器进行通信。


在老版本的IE5 和 IE6中使用的是ActiveX对象,其余的浏览器都是用 XMLHttpRequest对象。
下面来看一个简单的例子:
<span style="white-space:pre"></span>var xmlHttpRequest;if(window.XMLHttpRequest){xmlHttpRequest = new XMLHttpRequest();}else{xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");  //code for ie5 & ie6}

XMLHttpRequest为我们提供了open() 和 send() 方法将请求发送到服务器。


XMLHttpRequest.open(method,url,async);
描述:
method:请求的类型 (get 或者 post)
url:接受请求的服务器文件地址,也就是我们的请求发送到哪里去
asunc:true表示的是异步, false表示的是同步

一个简单的get请求:
<span style="white-space:pre"></span>XMLHttpRequest.open("GET","demo_get.php",true);<span style="white-space:pre"></span>XMLHttpRequest.send();

就是这样的。

一个简单的post请求:
<span style="white-space:pre"></span>XMLHttpRequest.open("POST","demo_get.php",true);XMLHttpRequest.send("username=Bill&password=123");


1 0
原创粉丝点击