问题:
server.js更新后,ie浏览器由于缓存问题,无法及时获取最新数据
解决:
修改html文件中以下代码,加上时间戳,浏览器才会认为是两次不一样的请求
xhr.open('GET','http://127.0.0.1:8000/ie?t='+Date.now());
HTML以及JS代码如下:
GET.html
<!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>ajax get请求</title>
<style>
#result {
width: 200px;
height: 100px;
border: 1px solid pink;
}
</style>
</head>
<body>
<button>点我发送请求</button>
<div id="result"></div>
<script>
// 获取元素
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById('result');
// 绑定事件
btn.onclick = function() {
// 1.创建对象
const xhr = new XMLHttpRequest();
// 2.初始化 设置请求方法和url
xhr.open('GET','http://127.0.0.1:8000/ie?t='+Date.now());
// 3.发送
xhr.send();
// 4.事件绑定 处理服务端返回的结果
// readystate是xhr对象中的属性,表示状态 0 1 2 3
xhr.onreadystatechange = function() {
// 判断相应状态码200 404 403 401 500
// 2xx 成功
if(xhr.readyState === 4) {
if(xhr.status >= 200 && xhr.status < 300) {
result.innerHTML = xhr.response;
}
}
}
}
</script>
</body>
</html>
server.js
// 引入
const { request } = require('express');
// 创建应用对象
const express = require('express');
// 创建应用对象
const app = express();
// 创建路由规则
app.get('/server',(request,response) => {
// 设置响应头 设置允许跨域
response.setHeader('Access-Control-Allow-Origin','*');
// 设置响应体
response.send('hello ajax~~');
});
app.get('/ie',(request,response) => {
// 设置响应头 设置允许跨域
response.setHeader('Access-Control-Allow-Origin','*');
// 设置响应体
response.send('hello ajax~ - 5');
});
// 监听端口启动服务
app.listen(8000,() => {
console.log("服务已启动,8000端口监听中...");
})