<!DOCTYPE html> <html lang="en">
<head> <Meta charset="UTF-8"> <Meta http-equiv="X-UA-Compatible" content="IE=edge"> <Meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head>
<body> <h1>你好</h1> <button>点我获取用户数据</button> </body> <script> const btn = document.querySelector('button'); btn.onclick = function () { const xhr = new XMLHttpRequest(); //同源可以省略,只写/data xhr.open('GET', '/data'); xhr.send(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if (xhr.status <= 200 && xhr.status < 300) { console.log(xhr.response); } } } }
</script>
</html> ==================server.js====================
const { request, response } = require('express'); const express = require('express'); const app = express(); app.get('/home', (request, response) => { response.sendFile(__dirname + '/index.html'); }) app.get('/data', (request, response) => {
const data = { name: 'xiaoming', age: '18' }; //setTimeout(() => { response.send(JSON.stringify(data)); //}, 3000);
})
app.listen(8000, () => { console.log('服务已经启动,8000端口监听中.......'); })
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。