AJAX发送POST请求的完整图文教程
作者:hjuan___
这篇文章主要介绍了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.查看浏览器的显示结果:


总结
到此这篇关于AJAX发送POST请求的文章就介绍到这了,更多相关AJAX发送POST请求内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
