AJAX: GET方法
例如:
data.PHP
- <?PHP
- $number = $_GET['num1'] + $_GET['num2'];
- echo $number;
- ?>
- <!doctype html>
- <html lang="en">
- <head>
- <Meta charset="UTF-8">
- <title>Document</title>
- <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");
- }
- function clickme () {
- document.getElementById('show').innerHTML = xmlhttp.responseText;
- }
- xmlhttp.open("GET","./data.PHP?num1=1&num2=2",true);
- xmlhttp.send();
- </SCRIPT>
- </head>
- <body>
- <input type="button" value="异步数据" onclick="clickme()">
- <div id="show"></div>
- </body>
- </html>
显示效果
AJAX :POST方法
data.PHP
- <?PHP
- $number = $_POST['num1'] + $_POST['num2'];
- echo $number;
- ?>
- <!doctype html>
- <html lang="en">
- <head>
- <Meta charset="UTF-8">
- <title>Document</title>
- <SCRIPT TYPE="text/javascript">
- var xmlhttp;
- if (window.XMLHttpRequest)
- {// code for IE7+,IE5
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- function clickme () {
- document.getElementById('show').innerHTML = xmlhttp.responseText;
- }
- xmlhttp.open("POST","./data.PHP",true);
- xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
- xmlhttp.send("num1=1&num2=2");
- </SCRIPT>
- </head>
- <body>
- <input type="button" value="异步数据" onclick="clickme()">
- <div id="show"></div>
- </body>
- </html>
效果图: