Ajax简单记录
1 Ajax
1.1 Ajax介绍
? Ajax全称为Asynchronous JavaScript And XML,就是异步的JS和XML。通过Ajax可以在浏览器中向服务器发送异步请求,最大的优势是:懒加载,用则加载,不用不加载,也就是无刷新获取数据。
1.2 XML介绍
? XML即可扩展标记语言,被设计用来传输和存储数据。XML和HTML类似,不同的是HTML都是预定义标签,而XML中没有预定义标签,全部都是自定义标签,用来表示一些数据。
// 在后端开发中最常见的XML文件,就是pom.xml
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
// 前端中XML
比如有一个单据信息:
id = 1; no = "x"; type = "正常";
用xml表示为:
<bill>
<id>1</id>
<no>x</no>
<type>正常</type>
</bill>
? 就目前来说,XML已经逐步被JSON取代
比如有一个单据信息:
id = 1; no = "x"; type = "正常";
用xml表示为:
{"id":1,"no":"x","type":"正常"}
1.3 Ajax特点
1.3.1 优点
1)可以无需刷新页面与服务器端进行通讯
2)允许根据用户事件(鼠标移动、点击等)来更新部分页面内容
1.3.2 缺点
1)没有浏览历史,不能回退
2)存在跨域问题
3)SEO(搜索引擎优化)不友好
1.4 HTTP
HTTP协议即超文本传输协议,协议详细规定了浏览器和万维网之间相互通信的规则
1.4.1 请求报文
行 POST/GET等 /s?id=utf-8 HTTP/1.1
头 Host: master.spunsugar.top
Cookie: name=master
Content-type: application/x-www-form-utlencoded
User-Agent: chrome 83
空行
体(Post请求需要写,Get不需要) username=admin&password=admin
1.4.2 响应报文
行 HTTP/1.1 200 OK
头 Content-Type: text/html;chartset=utf-8
Content-length: 2048
Content-encoding: gzip
空行
体 <html>
<head>
</head>
<body>
<h1>hello world</h1>
</body>
</html>
2 express框架
2.1 Express使用一
## 1.安装Node.js
## 2.使用Node.js的npm命令安装express
使用npm i express
PS D:\date\WebStrom\ajax> npm i express
npm notice created a lockfile as package-lock.json. You should commit this file.
PS D:\date\WebStrom\ajax> npm list
ajax@1.0.0 D:\date\WebStrom\ajax
`-- express@4.17.3
-
express.js
const express = require('express');
const app = express();
app.get('/',(request,response)=>{
response.send('Hello Express')
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
启动express
PS D:\date\WebStrom\ajax> node .\code\express.js
服务已经启动,8001端口监听
## 登录127.0.0.1:8001查看
2.2 Express使用二-Get请求
-
get.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width", initial-scale=1.0>
<title>Ajax Get 请求</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #90b;
}
</style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express')
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> node .\原生Ajax\server.js
服务已经启动,8001端口监听
2.3 Express使用三
-
get.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width", initial-scale=1.0>
<title>Ajax Get 请求</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #90b;
}
</style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById("result");
btn.onclick = function(){
console.log('Ajax');
const xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8001/server');
xhr.send();
xhr.onreadystatechange = function (){
if(xhr.readyState === 4){
console.log(xhr.status);
console.log(xhr.statusText);
console.log(xhr.getAllResponseHeaders());
console.log(xhr.response);
result.innerHTML = xhr.response;
} else {
}
}
}
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express')
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
查看get.html页面 查看get.html页面,通过点击button按钮,获取响应行,响应头,响应体,并将响应体返回显示在div标签中
2.4 Express使用四
-
get.html
//2.初始化 设置请求方法和url
xhr.open('GET','http://127.0.0.1:8001/server?a=100&b=200&c=300');
-
查看get.html页面 在url里添加请求参数,可在network中的request Headers 可以查看到/server/?a=100&b=200&c=300
2.5 Expres使用五-Post请求
-
post.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax Post 请求</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #903;
}
</style>
</head>
<body>
<div id="result"></div>
<script>
const result = document.getElementById("result");
result.addEventListener("mouseover",function () {
console.log("监控到鼠标放置到div标签中");
const xhr = new XMLHttpRequest();
xhr.open("POST","http://127.0.0.1:8001/server");
xhr.send();
xhr.onreadystatechange = function (){
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status <= 300){
result.innerHTML = xhr.response;
}else{
}
}
}
})
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express POST')
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
重启js PS D:\date\WebStrom\ajax> node .\原生Ajax\server.js
服务已经启动,8001端口监听
-
查看post.html
2.5 Express使用六-服务端响应Json数据
-
json.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax Json 响应数据</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #903;
}
</style>
</head>
<body>
<div id="result"></div>
<script>
const result = document.getElementById('result');
window.onkeydown = function () {
console.log('测试键盘按下事件');
const xhr = new XMLHttpRequest();
xhr.responseType = 'json';
xhr.open('GET','http://127.0.0.1:8001/json-server');
xhr.send();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status <= 300){
console.log(xhr.response);
console.log(xhr.response.name);
result.innerHTML = xhr.response.name;
}
}
}
}
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动express PS D:\date\WebStrom\ajax> node .\原生Ajax\server.js
服务已经启动,8001端口监听
-
补充说明 服务端:
服务端通过JSON.stringify()将对象转成json数据
HTML:
手动解析JSON:
//手动解析json
let data = JSON.parse(xhr.response);
// 取json中name对应的值
result.innerHTML = data.name;
自动解析JSON:
//设置响应体数据的类型,自动解析json数据
xhr.responseType = 'json';
result.innerHTML = xhr.responseType.name;
2.6 nodemon安装
在以上使用中,我们使用node server.js启动js,但是每次修改js内容,都需要重启,比较麻烦
使用nodemon管理之后,可以使用nodemon启动js,这样修改内容之后,只需要ctrl + c保存即可更新
nodemon是帮助开发基于node.js的应用程序的工具,可以在检测到目录中的文件更改时自动更新重启节点应用程序,不需要手动重启服务。
## 设置npm存储和缓存路径
PS D:\date\WebStrom\ajax> npm config set prefix="D:\software\nodejs\node_global"
PS D:\date\WebStrom\ajax> npm config set cache="D:\software\nodejs\node_cache"
## 方法一:使用这种方式下载,一直有问题,无法执行js文件
PS D:\date\WebStrom\ajax> npm install -g nodemon
## 方法二:最后使用这种方法,使用npm list也可以查看到下载到的nodemon
PS D:\date\WebStrom\ajax\原生Ajax> npm install --save-dev nodemon
PS D:\date\WebStrom\ajax\原生Ajax> nodemon .\server.js
[nodemon] 2.0.15
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node .\server.js`
服务已经启动,8001端口监听
## 一旦修改js文件,并保存后,nodemon会自动给重启服务
[nodemon] restarting due to changes...
[nodemon] starting `node .\server.js`
服务已经启动,8001端口监听
2.7 IE浏览器缓存问题
IE浏览器存在缓存问题,
当请求的URL为http://127.0.0.1/ie的时候,IE浏览器每次请求都会从缓存中取数据,导致数据更新不及时
所以在设置请求URL的时候,可以加上唯一标识,xhr.open("GET","http://127.0.0.1:8001/id?t="+Date.now())
-
ie.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax Json 响应数据</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #903;
}
</style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>
const btn = document.getElementsByTagName('button')[0];
const result = document.querySelector("#result");
btn.addEventListener('click',function () {
const xhr = new XMLHttpRequest();
xhr.open("GET","http://127.0.0.1:8001/ie?t="+Date.now());
xhr.send();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status <= 300){
result.innerHTML = xhr.response;
}
}
}
});
</script>
<script>
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST-2')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.get('/ie',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express Ie-2')
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> nodemon .\原生Ajax\server.js
2.8 网络连接超时
-
delay.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax 网络超时</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #903;
}
</style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>
const btn = document.getElementsByTagName('button')[0];
const result = document.querySelector("#result");
btn.addEventListener('click',function () {
const xhr = new XMLHttpRequest();
xhr.timeout = 2000;
xhr.ontimeout = function () {
alert("连接超时,请稍后重试");
};
xhr.onerror = function () {
alert("你的网络似乎出了小差...")
}
xhr.open("GET","http://127.0.0.1:8001/delay");
xhr.send();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status <= 300){
result.innerHTML = xhr.response;
}
}
}
});
</script>
<script>
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST-2')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.get('/ie',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express Ie-2')
});
app.get('/delay',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
setTimeout(() => {
response.send('延迟响应');
},3000);
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> nodemon .\原生Ajax\server.js
-
网络连接超时测试
xhr.ontimeout = function () {
alert("连接超时,请稍后重试");
};
-
网络异常测试
xhr.onerror = function () {
alert("你的网络似乎出了小差...")
}
-
chrome设置-模拟离线状态 -
测试 ?
2.9 Ajax请求/取消
-
cancel.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax 请求/取消</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #903;
}
</style>
</head>
<body>
<button>请求</button>
<button>取消</button>
<script>
const btns = document.querySelectorAll("button");
let xhr = null;
btns[0].onclick = function () {
xhr = new XMLHttpRequest();
xhr.open("GET","http://127.0.0.1:8001/delay");
xhr.send();
}
btns[1].onclick = function () {
xhr.abort();
}
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST-2')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.get('/ie',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express Ie-2')
});
app.get('/delay',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
setTimeout(() => {
response.send('延迟响应');
},3000);
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> nodemon .\原生Ajax\server.js
?
2.10 Ajax重复发送请求
-
repeat.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ajax 重复发送请求</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #903;
}
</style>
</head>
<body>
<button>请求</button>
<script>
const btns = document.querySelectorAll("button");
let xhr = null;
let isSending = false;
btns[0].onclick = function () {
if(isSending) xhr.abort();
xhr = new XMLHttpRequest();
isSending = true;
xhr.open("GET","http://127.0.0.1:8001/delay");
xhr.send();
xhr.onreadystatechange = function () {
if(xhr.readyState === 4) {
isSending = false;
}
}
}
btns[1].onclick = function () {
xhr.abort();
}
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST-2')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.get('/ie',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express Ie-2')
});
app.get('/delay',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
setTimeout(() => {
response.send('延迟响应');
},3000);
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> nodemon .\原生Ajax\server.js
?
3.JQuary中的Ajax
3.1 get请求
$.get(url,[data],[callback],[type])
url:请求的URL地址
data:请求携带的参数
callback:载入成功时回调函数
type:设置返回内容格式,xml\html\script\json\text\_default
3.2 post请求
$.post(url,[data],[callback],[type])
url:请求的URL地址
data:请求携带的参数
callback:载入成功时回调函数
3.3 jQuery发送Ajax的post和get请求
-
jquery-ajax.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<title>jQuery 发送 Ajax 请求</title>
</head>
<body>
<div class="container">
<h2 class="page-header">jQuery发送Ajax请求</h2>
<button class="btn btn-primary">GET</button>
<button class="btn btn-danger">POST</button>
<button class="btn btn-info">通用型方法ajax</button>
</div>
<script>
$('button').eq(0).click(function () {
$.get('http://127.0.0.1:8001/jquery',{a:100,b:200},function (data) {
console.log(data);
},'json');
})
$('button').eq(1).click(function () {
$.get('http://127.0.0.1:8001/jquery',{a:100,b:200},function (data) {
console.log(data);
},'json');
})
$('button').eq(2).click(function () {
$.ajax({
url: 'http://127.0.0.1:8001/jquery',
data: {a:100,b:200},
type: 'GET',
dataType: 'json',
success: function (data) {
console.log(data);
},
timeout: 2000,
error: function () {
console.log("请求超时")
},
headers: {
c:300,
d:400
}
})
})
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST-2')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.get('/ie',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express Ie-2')
});
app.get('/delay',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
setTimeout(() => {
response.send('延迟响应');
},3000);
});
app.all('/jquery',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {name: "Ajax",language:"jQuery"};
response.send(JSON.stringify(data));
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> nodemon .\原生Ajax\server.js
?
4.Axios发送请求
-
axios.html <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.26.1/axios.min.js"></script>
<title>Axios 发送请求</title>
</head>
<body>
<button>GET</button>
<button>POST</button>
<button>AJAX</button>
<script>
const btns = document.querySelectorAll('button');
axios.defaults.baseURL = 'http://127.0.0.1:8001'
btns[0].onclick = function () {
axios.get('/axios-server',{
params: {
id:100,
vip:7
},
headers: {
name:'axios',
type:'json'
}
}).then(value => {
console.log(value.data);
});
}
btns[1].onclick = function () {
axios.post('/axios-server',{
name: 'Ajax',
language:'jQuery'
},
{
params: {
id:100,
vip:7
},
headers: {
name:'axios',
type:'json'
}
}).then(value => {
console.log(value.data);
});
}
btns[2].onclick = function () {
axios({
method: 'POST',
url: '/axios-server',
params: {
a:100,
b:200
},
headers: {
c:300,
d:400
},
data: {
username: 'admin',
password: 'admin'
}
}).then(response => {
console.log("响应码:"+response.status);
console.log(response.data);
})
}
</script>
</body>
</html>
-
server.js
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('Hello Express GET')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express POST-2')
});
app.get('/json-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {
name: 'Ajax_Json'
};
let str = JSON.stringify(data);
response.send(data);
});
app.get('/ie',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
response.send('Hello Express Ie-2')
});
app.get('/delay',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
setTimeout(() => {
response.send('延迟响应');
},3000);
});
app.all('/jquery',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {name: "Ajax",language:"jQuery"};
response.send(JSON.stringify(data));
});
app.all('/axios-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {name: "Ajax",language:"jQuery"};
response.send(JSON.stringify(data));
});
app.listen(8001,()=>{
console.log("服务已经启动,8001端口监听")
});
-
启动 PS D:\date\WebStrom\ajax> nodemon .\原生Ajax\server.js
?
5.BootCDN
稳定、快速、免费的前端开源项目CDN加速服务
BootCDN
|