test.html 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <h3>这是个测试文件</h3>
  11. </body>
  12. </html>
  13. <script>
  14. function post() {
  15. var xmlhttp;
  16. if(window.XMLHttpRequest) {
  17. xmlhttp = new XMLHttpRequest();
  18. }else if(window.ActiveXObject) {
  19. xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
  20. }
  21. //回调
  22. xmlhttp.onreadystatechange = function(){
  23. if(xmlhttp.readyState == 4) {
  24. if(xmlhttp.status == 304 || (xmlhttp.status >= 200 && xmlhttp.status < 300)) {
  25. var renderMessage = JSON.parse(xmlhttp.responseText);
  26. console.log(renderMessage);
  27. }
  28. }
  29. }.bind(this);
  30. //请求
  31. xmlhttp.open('post', '/test', true);
  32. xmlhttp.setRequestHeader('Content-Type','application/json;charset=utf-8');
  33. xmlhttp.send(JSON.stringify({id:'12321', params: 'qwe'}));
  34. }
  35. function get() {
  36. var xmlhttp;
  37. if(window.XMLHttpRequest) {
  38. xmlhttp = new XMLHttpRequest();
  39. }else if(window.ActiveXObject) {
  40. xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
  41. }
  42. //回调
  43. xmlhttp.onreadystatechange = function(){
  44. if(xmlhttp.readyState == 4) {
  45. if(xmlhttp.status == 304 || (xmlhttp.status >= 200 && xmlhttp.status < 300)) {
  46. var renderMessage = JSON.parse(xmlhttp.responseText);
  47. console.log(renderMessage);
  48. }
  49. }
  50. }.bind(this);
  51. //请求
  52. xmlhttp.open('get', '/test?name=213&sex=1', true);
  53. xmlhttp.setRequestHeader('Content-Type','application/json;charset=utf-8');
  54. xmlhttp.send();
  55. }
  56. </script>