json数据:https://mock.mengxuegu.com/
json数据:https://myjson.dit.upm.es/
前端代码
<!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>
<button onclick="addajax()">ajax请求</button>
<script>
// ajax 请求 本地JSON文件
function ajax(callback) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText, '====responseText')
// var myObj = JSON.parse(this.responseText);
if (callback && typeof callback == "function") {
callback(this.responseText);
}
console.log(this.responseText);
}
};
xmlhttp.open("GET", "http://127.0.0.1:3000/fang", true);
xmlhttp.send();
}
function addajax() {
ajax((res) => {
console.log(res, '===res')
})
}
</script>
</body>
</html>
<!-- <!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>node中的ajax请求(html5页面)</title>
<script type="text/javascript">
function GetData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "http://localhost:1337/", true);
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
document.getElementById("res").innerHTML = xhr.responseText;
}
}
}
xhr.send(null);
}
</script>
</head>
<body>
<input type="button" value="获取数据" onclick="GetData()" />
<div id="res">dsdf</div>
</body>
</html> -->
node 后台代码
// fs.readFile("./1.json", "utf8", function (err, data) {
//
// // let str = data.trim().replace(/\s+/g, "");
// 除去空格
// function Trim(str, is_global = "i") {
// var result;
// result = str.replace(/(^\s+)|(\s+$)/g, "");
// if (is_global.toLowerCase() == "g") result = result.replace(/\s/g, "");
// return result;
// }
// fs.writeFile("./11.json", Trim(data, "g"), function (err) {
// if (err) console.log("写文件操作失败");
// else console.log("写文件操作成功");
// });
// // console.log(Trim(data, "g"));
// });
// var data = fs.readFileSync("./test.txt", "utf8");
// console.log(data);
var http = require("http");
// req 路由监听空 res 上下文函数
var server = http.createServer((req, res) => {
// 公共请求头
res.writeHead(200, {
"Content-Type": "text/html;chaset=UTF-8",
//设置允许跨域的域名,*代表允许任意域名跨域
"Access-Control-Allow-Origin": "*",
});
// 路由监控
if (req.url == "/fang") {
res.end("fang");
} else if (req.url == "/yuan") {
res.end("yuan");
} else {
// res.end("404");
res.end("<a href='/fang'>fang</a><br><a href='yuan'>yuan</a>");
}
});
server.listen(3000, "127.0.0.1", () => {
console.log("http://127.0.0.1:3000/");
});
// var http = require("http");
// var server = http.createServer(function (req, res) {
// if (req.url !== "/favicon.ico") {
// res.writeHead(200, {
// "Content-Type": "text/plain",
// "Access-Control-Allow-Origin": "*",
// });
// res.write("你好啊!");
// }
// res.end();
// });
// server.listen(1337, "localhost", function () {
// console.log("开始监听...");
// });
参考:https://www.jb51.net/article/57874.htm
//引入express
const express = require('express')
//端口号
const port = 8888
//创建应用对象
const app = express()
//创建路由规则 路由就是引路人 根据不同的url请求 导航到不同的页面
app.get('/server', (request, response) => {
//request 是对请求报文的封装
//response 是对响应报文的封装
//设置响应头 允许跨域
response.setHeader('Access-Control-Allow-Origin', '*')
//设置响应体
response.send('Hello Ajax,I am dingqiusi->get')
})
//监听端口 启动服务
app.listen(port, () => {
console.log(`服务已经启动了,端口号:${port}`)
})