ajax发送get和post请求

待我称王封你为后i 2023-10-06 17:19 112阅读 0赞

express.js文件

  1. //1. 引入express
  2. const express = require('express');
  3. //2. 创建应用对象
  4. const app = express();
  5. //3. 创建路由规则
  6. // request 是对请求报文的封装
  7. // response 是对响应报文的封装
  8. app.get('/server', (request, response)=>{
  9. //设置响应头 设置允许跨域
  10. response.setHeader('Access-Control-Allow-Origin', '*');
  11. //设置响应体
  12. response.send('HELLO AJAX - 2');});
  13. app.post('/server', (request, response)=>{
  14. //设置响应头 设置允许跨域
  15. response.setHeader('Access-Control-Allow-Origin', '*');
  16. //设置响应体
  17. response.send('HELLO AJAX POST');
  18. });
  19. //4. 监听端口启动服务
  20. app.listen(8000, ()=>{
  21. console.log("服务已经启动, 8000 端口监听中....");
  22. });

get请求

  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. <title>AJAX GET 请求</title>
  7. <style>
  8. #result{
  9. width:200px;
  10. height:100px;
  11. border:solid 1px #90b;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <button>点击发送请求</button>
  17. <div id="result"></div>
  18. <script>
  19. //获取button元素
  20. const btn = document.getElementsByTagName('button')[0];
  21. const result = document.getElementById("result");
  22. //绑定事件
  23. btn.onclick = function(){
  24. //1. 创建对象
  25. const xhr = new XMLHttpRequest();
  26. //2. 初始化 设置请求方法和 url
  27. xhr.open('GET', 'http://127.0.0.1:8000/server?a=100&b=200&c=300');
  28. //3. 发送
  29. xhr.send();
  30. //4. 事件绑定 处理服务端返回的结果
  31. // on when 当....时候
  32. // readystate 是 xhr 对象中的属性, 表示状态 0 1 2 3 4
  33. //0:未初始化,1:open()方法执行完毕,2:send()方法执行完毕,3:服务端返回部分结果,4:服务端返回全部结果
  34. // change 改变
  35. xhr.onreadystatechange = function(){
  36. //判断 (服务端返回了所有的结果)
  37. if(xhr.readyState === 4){
  38. //判断响应状态码 200 404 403 401 500
  39. // 2xx 成功
  40. if(xhr.status >= 200 && xhr.status < 300){
  41. //处理结果 行 头 空行 体
  42. //1、响应行
  43. // console.log(xhr.status);//状态码
  44. // console.log(xhr.statusText);//状态字符串
  45. // console.log(xhr.getAllResponseHeaders());//所有响应头
  46. // console.log(xhr.response);//响应体
  47. //设置 result 的文本
  48. result.innerHTML = xhr.response;
  49. }else{
  50. }
  51. }
  52. }
  53. }
  54. </script>
  55. </body>
  56. </html>

post请求

  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. <title>AJAX POST 请求</title>
  7. <style>
  8. #result{
  9. width:200px;
  10. height:100px;
  11. border:solid 1px #903;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <div id="result"></div>
  17. <script>
  18. //获取元素对象
  19. const result = document.getElementById("result");
  20. //绑定事件
  21. result.addEventListener("mouseover", function(){
  22. //1. 创建对象
  23. const xhr = new XMLHttpRequest();
  24. //2. 初始化 设置类型与 URL
  25. xhr.open('POST', 'http://127.0.0.1:8000/server');
  26. //3. 发送
  27. xhr.send('a=100&b=200&c=300');
  28. // xhr.send('a:100&b:200&c:300');
  29. // xhr.send('1233211234567');
  30. //4. 事件绑定
  31. xhr.onreadystatechange = function(){
  32. //判断
  33. if(xhr.readyState === 4){
  34. if(xhr.status >= 200 && xhr.status < 300){
  35. //处理服务端返回的结果
  36. result.innerHTML = xhr.response;
  37. }
  38. }
  39. }
  40. });
  41. </script>
  42. </body>
  43. </html>

发表评论

表情:
评论列表 (有 0 条评论,112人围观)

还没有评论,来说两句吧...

相关阅读