【04】AJAX发送POST请求

一、发送POST请求

1.test.html中的内容:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">点击发送请求</button>
    <div id="dd"></div>
    <script>
      let btn = document.getElementById("btn");
      let dd = document.getElementById("dd");
      btn.onclick = function () {
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "http://127.0.0.1:8000");
        xhr.send("a=100&b=200&c=300");
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
              dd.innerHTML = xhr.response; //将响应体中的内容添加到文本框dd中
            }
          }
        };
      };
    </script>
  </body>
</html>

test.js中的内容:

const express = require("express");
const app = express();
app.get("/", (request, response) => {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.send("HELLO EXPRESS");
});
app.post("/", (request, response) => {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.send("HELLO EXPRESS");
});
app.listen(8000, () => {
  console.log("服务启动,800端口监听中……");
});


2.查看浏览器的显示结果:

二、设置请求体

1.test.html中的内容:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">点击发送请求</button>
    <div id="dd"></div>
    <script>
      let btn = document.getElementById("btn");
      let dd = document.getElementById("dd");
      btn.onclick = function () {
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "http://127.0.0.1:8000");
        //设置请求头  (头的名字,头的值)
        xhr.setRequestHeader(
          "Content-Type",
          "application/x-www-form-urlencoded"
        );
        // 发送
        xhr.send("a=100&b=200&c=300");
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
              dd.innerHTML = xhr.response; 
            }
          }
        };
      };
    </script>
  </body>
</html>

test.js中的内容:

const express = require("express");
const app = express();
app.get("/", (request, response) => {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.send("HELLO EXPRESS");
});
app.POST("/", (request, response) => {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.send("HELLO EXPRESS");
});
app.listen(8000, () => {
  console.log("服务启动,800端口监听中……");
});

2.查看浏览器的显示结果:

三、设置自定义请求头

1.test.html中的内容:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button id="btn">点击发送请求</button>
    <div id="dd"></div>
    <script>
      let btn = document.getElementById("btn");
      let dd = document.getElementById("dd");
      btn.onclick = function () {
        const xhr = new XMLHttpRequest();
        xhr.open("POST", "http://127.0.0.1:8000/server");
        //设置自定义请求头
        xhr.setRequestHeader(
          "Content-Type",
          "application/x-www-form-urlencoded"
        );
        xhr.setRequestHeader("name", "xiaohong");
        // 发送
        xhr.send("a=100&b=200&c=300");
        xhr.onreadystatechange = function () {
          if (xhr.readyState === 4) {
            if (xhr.status >= 200 && xhr.status < 300) {
              dd.innerHTML = xhr.response; //将响应体中的内容添加到文本框dd中
            }
          }
        };
      };
    </script>
  </body>
</html>

test.js中的内容:

const express = require("express");
const app = express();
app.get("/server", (request, response) => {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.send("HELLO EXPRESS");
});

app.all("/server", (request, response) => {
  response.setHeader("Access-Control-Allow-Origin", "*");
  response.setHeader("Access-Control-Allow-Headers", "*");
  response.send("HELLO EXPRESS");
});
app.listen(8000, () => {
  console.log("服务启动,800端口监听中……");
});

2.查看浏览器的显示结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值