ajax在js中的运用

<script>
var xmlhttp;
function loadXMLDoc(url,cfunc)
{

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


xmlhttp.onreadystatechange=cfunc;

xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction1(str)
{


loadXMLDoc("http://localhost/Project1/Select.php?name="+str,function()
{

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



if(xmlhttp.responseText=="yes"){
document.getElementById("name1").innerHTML = "用户名已存在";}
else{
document.getElementById("name1").innerHTML = "用户名正确";}



}
});
}

1.创建 XMLHttpRequest 对象

所有现代浏览器(IE7+、Firefox、Chrome、Safari 以及 Opera)均内建 XMLHttpRequest 对象。

创建 XMLHttpRequest 对象的语法:

variable=new XMLHttpRequest();

老版本的 Internet Explorer (IE5 和 IE6)使用 ActiveX 对象:

variable=new ActiveXObject("Microsoft.XMLHTTP");

为了应对所有的现代浏览器,包括 IE5 和 IE6,请检查浏览器是否支持 XMLHttpRequest 对象。如果支持,则创建 XMLHttpRequest 对象。如果不支持,则创建 ActiveXObjec

二向服务器发送请求

如需将请求发送到服务器,我们使用 XMLHttpRequest 对象的 open() 和 send() 方法:

xmlhttp.open("GET","ajax_info.txt",true);
xmlhttp.send();

方法 描述
open(method,url,async)

规定请求的类型、URL 以及是否异步处理请求。

  • method:请求的类型;GET 或 POST
  • url:文件在服务器上的位置
  • async:true(异步)或 false(同步)
send(string)

将请求发送到服务器。

  • string:仅用于 POST 请求

猜你喜欢

转载自www.cnblogs.com/insist-bin/p/11025292.html