|
html入口文件:
- <html>
- <head>
- <title>ajax请求PHP数据</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <!-- ajax 请求 php 执行流程分析 -->
- <!--
- 1、为了应对所有的现代浏览器,包括 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");
- }
- 2、执行的回调函数
- xmlHttp.onreadystatechange = stateChanged; //stateChanged是指回调函数
- 3、请求数据
- //GET方式
- xmlHttp.open("GET",url,true);
- xmlHttp.send(null);
- //POST方式
- var data = "url=http://www.baidu.com&id=5";
- xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //POST数据必须加上这一句 ,定义POST传输的文件HTTP头信息
- xmlHttp.send(data);
- 4、回调函数
- function stateChanged()
- {
- if (xmlHttp.readyState == 4) //如果状态码为 4 则表示成功
- {
- document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
- }
- }
- -->
- </head>
-
- <body>
- <div id="show">数据加载中......</div>
- </body>
- <script type="text/javascript">
-
- // 文件载入完成后创建对象
- 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 = insertData;
-
- //请求数据
- var url = "data.php?num=" + Math.random(); //防止浏览器认为URL没发生更改
- xmlHttp.open("POST",url,true); //用get方法传递数据到请求页面
- var data = "url=http://www.baidu.com&id=5";
- xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); //POST数据必须加上这一句 ,定义POST传输的文件HTTP头信息
- xmlHttp.send(data);
- //alert(1);
- //回调函数
- function insertData()
- {
- if (xmlHttp.readyState==4)
- {
- document.getElementById("show").innerHTML = xmlHttp.responseText; //xmlHttp.responseText 获取输出内容
- }
- }
- </script>
- </html>
复制代码 data.php文件:
- <?php
- for($i=0;$i<60000;$i++)
- {
- echo "Data=:".$_POST['url']."--".$_POST['id'] ."<br>";
- }
- ?>
复制代码
|
|