const express = require("express");
const app = express();
const port = 4000;
app.all('*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type');
res.header('Access-Control-Allow-Methods', '*');
res.header('Content-Type', 'application/json;charset=utf-8');
next();
});
app.get("/", (req, res) => {
console.log(112)
res.json({
code: "连接成功",
});
});
app.listen(port);
<!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>
</body>
<script>
queryFn()
function queryFn(){
console.log(1122)
let xhr = new XMLHttpRequest();
xhr.open("get", "http://localhost:4000", true);
xhr.onload = function() {
console.log(xhr.response);
};
xhr.send();
}
</script>
</html>