README
npm init -y
npm install express
app.js 服务器端
const express = require('express');
const app = express();
app.use(express.static('public'));
app.listen(8080,()=>{console.log('http://127.0.0.1:8080/')});
app.get('/getUser', function (req, res) {
res.send({name: 'zhangsan', password: 123456});
});
public>index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<script src="./index.js"></script>
</head>
<body>
<div></div>
</body>
</html>
public>index.css
body{
background-color: skyblue;
}
public>index.js 原生Ajax
const req = new XMLHttpRequest();
req.open('GET', '/getUser');
req.send();
req.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
document.querySelector('div').innerHTML = this.responseText;
}
}