Ajax
XML
ajax设置请求参数
运行:node server.js
//1-get.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ajax get请求</title>
<style>
#result{
width: 12.5rem;
height: 6.25rem;
border: solid 1px aquamarine;
}
</style>
</head>
<body>
<button>点击发送请求</button>
<div id="result"></div>
<script>
const btn = document.getElementsByTagName('button')[0];
const result = document.getElementById("result");
btn.onclick = function(){
const xhr = new XMLHttpRequest();
xhr.open('GET','http://127.0.0.1:8000/server?a=100&b=200&c=300');
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status >=200 && xhr.status < 300){
result.innerHTML = xhr.response;
}else{
}
}
}
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AJAX POST请求</title>
<style>
#result{
width: 200px;
height: 100px;\
border: solid 1px #7FFFD4;
}
</style>
</head>
<body>
<div id="result"></div>
<script>
const result = document.getElementById("result");
result.addEventListener("mouseover",function(){
const xhr = new XMLHttpRequest();
xhr.open('POST','http://127.0.0.1:8000/server');
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status < 300){
result.innerHTML = xhr.response;
}
}
}
});
</script>
</body>
</html>
const express = require('express');
const app = express();
app.get('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('hello')
});
app.post('/server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.send('hello post');
})
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中。。。");
})
post设置请求体
//3.发送
xhr.send('a=100&b=200&c=300');
// xhr.send('a:100&b:200&c:300');
设置请求头
//设置请求头
xhr.setRequestHeader('Content-Type','application/x-www-from-urlencoded');
nodemon
Nodemon 是一款非常实用的工具,用来监控你 node.js 源代码的任何变化和自动重启你的服务器。 Nodemon 是一款完美的开发工具,可以使用 npm 安装。
安装
npm install -g nodemon
对server.js文件开启nodemon服务
nodemon server.js
处理超时问题
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>AJAX POST请求</title>
<style>
#result{
width: 200px;
height: 100px;
border: solid 1px #7FFFD4;
}
</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:8000/timeout');
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4){
if(xhr.status >= 200 && xhr.status < 300){
result.innerHTML = xhr.response;
}
}
}
})
</script>
</body>
</html>
const express = require('express');
const app = express();
app.all('/timeout',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
setTimeout(() => {
response.send('延时响应');
},3000)
});
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中。。。");
})
取消请求
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>取消请求</title>
</head>
<body>
<button>点击发送</button>
<button>点击取消</button>
<script>
const btns = document.querySelectorAll('button');
let x = null;
btns[0].onclick = function(){
x = new XMLHttpRequest();
x.open("GET",'http://127.0.0.1:8000/timeout');
x.send();
}
btns[1].onclick = function(){
x.abort();
}
</script>
</body>
</html>
const express = require('express');
const app = express();
app.all('/timeout',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
setTimeout(() => {
response.send('延时响应');
},3000)
});
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中。。。");
})
处理重复请求(标识一个变量解决
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>重复请求</title>
</head>
<body>
<button>点击发送</button>
<script>
const btns = document.querySelectorAll('button');
let x = null;
let isSending = false;
btns[0].onclick = function(){
if(isSending) x.abort();
x = new XMLHttpRequest();
isSending = true;
x.open("GET",'http://127.0.0.1:8000/timeout');
x.send();
x.onreadystatechange = function(){
if(x.readyState === 4){
isSending = false;
}
}
}
</script>
</body>
</html>
const express = require('express');
const app = express();
app.all('/timeout',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
setTimeout(() => {
response.send('延时响应');
},3000)
});
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中。。。");
})
jquery发送ajax请求
//ajax.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jquery发送ajax请求</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<div>
<h2>jquery发送ajax请求</h2>
<button>GET</button>
<button>POST</button>
<button>通用型方法</button>
</div>
<script>
$('button').eq(0).click(function(){
$.get('http://127.0.0.1:8000/jquery-server', {a:100,b:200}, function(data){
console.log(data);
},'json');
})
$('button').eq(1).click(function(){
$.post('http://127.0.0.1:8000/jquery-server', {a:100,b:200}, function(data){
console.log(data);
},'json');
})
$('button').eq(2).click(function(){
$.ajax({
url:'http://127.0.0.1:8000/jquery-server',
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>
const express = require('express');
const app = express();
app.all('/jquery-server',(request,response)=>{
response.setHeader('Access-Control-Allow-Origin','*');
response.setHeader('Access-Control-Allow-Headers','*');
const data = {name: '尚硅谷'};
response.send(JSON.stringify(data));
});
app.listen(8000,()=>{
console.log("服务已经启动,8000端口监听中。。。");
})
|