js基础知识示例—js ajax请求

来源:互联网 发布:象棋拆棋软件 编辑:程序博客网 时间:2024/06/05 04:02

demo.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script language="javascript" type="text/javascript">//通过这个函数来异步获取信息function  Ajax(){    var xmlHttpReq =null;    if(xmlHttpReq.ActiveXObject){        xmlHttpReq=new ActiveXObject('Microsoft.XMLHTTP');    }else if(windows.XMLHttpRequest){        xmlHttpReq=new XMLHttpRequest();    }    if(xmlHttpReq!==null){        xmlHttpReq.open('GET','test.php',true);        xmlHttpReq.onreadystatechange=RequestCallback;        xmlHttpReq.send();    }    function  RequestCallBack() {       if(xmlHttpReq.readyState==4){           if(xmlHttpReq.status==200){               document.getElementById('resText').innerHTML=xmlHttpReq.responseText;           }       }    }}</script></head><body><input type="button" value="Ajax提交" onclick="Ajax();" /><div id="resText" ></div></body></html>

test.php

<?php   echo "Hello Ajax!";?>
原创粉丝点击