AJAX post get

前端之家收集整理的这篇文章主要介绍了AJAX post get前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

AJAX: GET方法

例如:

data.PHP

  1. <?PHP
  2. $number = $_GET['num1'] + $_GET['num2'];
  3.  
  4. echo $number;
  5.  
  6. ?>

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>Document</title>
  6. <SCRIPT TYPE="text/javascript">
  7. var xmlhttp;
  8. if (window.XMLHttpRequest)
  9. {// code for IE7+,Firefox,Chrome,Opera,Safari
  10. xmlhttp=new XMLHttpRequest();
  11. }
  12. else
  13. {// code for IE6,IE5
  14. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16.  
  17. function clickme () {
  18. document.getElementById('show').innerHTML = xmlhttp.responseText;
  19. }
  20. xmlhttp.open("GET","./data.PHP?num1=1&num2=2",true);
  21. xmlhttp.send();
  22. </SCRIPT>
  23. </head>
  24. <body>
  25. <input type="button" value="异步数据" onclick="clickme()">
  26. <div id="show"></div>
  27. </body>
  28. </html>

显示效果

AJAX :POST方法

data.PHP

  1. <?PHP
  2. $number = $_POST['num1'] + $_POST['num2'];
  3.  
  4. echo $number;
  5.  
  6. ?>

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <Meta charset="UTF-8">
  5. <title>Document</title>
  6. <SCRIPT TYPE="text/javascript">
  7. var xmlhttp;
  8. if (window.XMLHttpRequest)
  9. {// code for IE7+,IE5
  10. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  11. }
  12.  
  13. function clickme () {
  14. document.getElementById('show').innerHTML = xmlhttp.responseText;
  15. }
  16. xmlhttp.open("POST","./data.PHP",true);
  17. xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  18. xmlhttp.send("num1=1&num2=2");
  19. </SCRIPT>
  20. </head>
  21. <body>
  22. <input type="button" value="异步数据" onclick="clickme()">
  23. <div id="show"></div>
  24. </body>
  25. </html>

效果图:

猜你在找的Ajax相关文章