加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ajax在js中的运用

发布时间:2020-12-16 02:50:51 所属栏目:百科 来源:网络整理
导读: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.onrea

<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 请求

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读