ajaxs

<?php 
header("Content-Type:text/html; charset=utf-8");
?>
<html>
<script type="text/javascript">
function loadXMLDoc(){
<!--
//创建ajax对象。
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");
  }
var url='http://www.w3school.com.cn/ajax/ajax_xmlhttprequest_send.asp';

xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
  {
  document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
  }
}
xmlhttp.open("GET","./newfile.php",true);
xmlhttp.send();
}
//-->
</script>


<style>
<!--
div{
border:1 solid red;
width:300px;
height:300px;
}
//-->
</style>
<body>
<div>
这边我不要变。

</div>
<div id='myDiv'>
刷新好吗?
<button type="button" onclick="loadXMLDoc()">请求数据</button>  

</div>

</body>

</html>

ajaxs基本用法。

url="www.baidu.com?t="+Math.random();
//避免缓存误导。
var xmlhttp.open("GET",url,true);
xmlhttp.send();
 

 

ajax 提交POST数据

<html>
<meta http-equiv="Content-Type" charset="UTF-8"/>
<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("myDiv").innerHTML=xmlhttp.responseText;
                }
            }

            xmlhttp.open("POST","./result.php",true);
            xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            xmlhttp.send("user=xie&password=123456");
        }
    </script>
</head>
<body>

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

</body>
</html>

 

 

 

posted @ 2014-04-29 10:53  UCanBeFree  阅读(444)  评论(0编辑  收藏  举报