首页 > 代码库 > php javascript的ajax

php javascript的ajax

先说基础一点的get类型的ajax

function loadXMLDoc(){var xmlhttp;
//首先判断浏览器是否支持xmlhttprequest,因为ie56不是这个对象,是activexobject
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); }else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
//然后是当readystate改变的时候,判断状态是否正常,如果正常说明请求完成了,就进行请求后的操作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();

下面是post类型,区别不大

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("POST","http://www.weisuyun.com/xiaochengxu.php",true);//GET改为POSTxmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//这是添加http的头xmlhttp.send("name=Bill&sex=Gates");//send里面写参数

最后是稍微整合一下的

<html><head><script type="text/javascript">var xmlhttp;function loadXMLDoc(url,cfunc){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=cfunc;xmlhttp.open("GET",url,true);xmlhttp.send();}function myFunction(){loadXMLDoc("/ajax/test1.txt",function()  {  if (xmlhttp.readyState==4 && xmlhttp.status==200)    {    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;    }  });}</script></head><body><div id="myDiv"><h2>Let AJAX change this text</h2></div><button type="button" onclick="myFunction()">通过 AJAX 改变内容</button></body></html>

 

php javascript的ajax