侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

ajax使用

2024-05-15 星期三 / 0 评论 / 0 点赞 / 82 阅读 / 2526 字

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

创建 XMLHttpRequest 对象(转自W3C手册)

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

创建 XMLHttpRequest 对象的语法:

variable=new XMLHttpRequest();

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

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

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

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");  }

下面是完整请求实例:

<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("box").innerHTML=xmlhttp.responseText;
      }
  }
xmlhttp.open("GET","xxxx.php?name=jack&age=18",true);

//使用POST发送

//xmlhttp.open("POST","ajax_test.asp",true);//xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//xmlhttp.send(" name=jack&age=18");

xmlhttp.send();
}
</script>
</head>
<body>

<h2>AJAX   Test</h2>
<button type="button" onclick="loadXMLDoc()">请求数据</button>
<div id="box"></div>

</body>
</html>

使用jQuery的ajax调用简单实例

$.ajax({   type: "POST",   url: "some.php",   data: "name=John&location=Boston",   success: function(msg){     alert( "Data Saved: " + msg );   }});

 

广告 广告

评论区