1 回答
TA贡献1784条经验 获得超9个赞
您可以使用 nodeJS 和expressJS来实现它。要安装快速运行npm i express.
试试这个
let http = require('http');
let fs = require('fs');
let express = require('express');
let app = express();
app.get('/', function (req, res) {
res.sendfile('./index.html');
})
app.get('/getData', function (req, res) {
console.log("getData called.");
res.send("res from getData function");
})
var server = app.listen(8000, function () {
var host = server.address().address
var port = server.address().port
console.log("Example app listening at http://%s:%s", host, port)
})
<html>
<head>
<script>
function httpGet(theUrl) {
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:8000/getData";
xmlhttp.onreadystatechange = function (res) {
if (this.readyState == 4 && this.status == 200) {
document.write(this.responseText);
}
};
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
</script>
</head>
<body>
<button onclick="httpGet()">httpGet</button>
</body>
</html>
添加回答
举报