原生AJAX服务请求

服务端

//1.引入(利用express框架)
const express = require('express');
// const { request } = require('http');

//2.创建应用对象
const app = express();

//3.创建路由规则
//response 是对响应报文的封装
//request 是对请求报文的封装

app.all('/server',(request,response)=>{
    //设置响应头,设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');

    response.setHeader('Access-Control-Allow-Headers','*');
    //设置响应
    response.send('HELLO AJAX GET');
});
//all表示可以接收任意类型的请求
app.all('/json-server',(request,response)=>{
    //设置响应头,设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');
    //设置响应头(所有信息的头信息都可以接收)
    response.setHeader('Access-Control-Allow-Headers','*');
    //响应一个数据
    const date = {
        name: 'chen'
    };
    //对对象进行字符串转换
    let str = JSON.stringify(date);
    //设置响应体
    response.send(str);
});
//针对IE缓存
app.get('/ie',(request,response)=>{
    //设置响应头,设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    //设置响应
    response.send('HELLO IE');
});

//针对延时响应
app.get('/delay',(request,response)=>{
    //设置响应头,设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');
    response.setHeader('Access-Control-Allow-Headers','*');
    //设置响应体
    setTimeout(()=>{
        response.send('延时响应');
    },3000)
    
});

//4.监听端口并启动服务
app.listen(8080,()=>{
    console.log("服务已经启动,8080 端口监听中...");
});

请求端

缓存问题

<!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>缓存问题</title>
    <style>
        #result{
            width: 200px;
            height: 100px;
            border: solid 1px #258;
        }
    </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:8080/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>
</body>
</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>IE缓存问题</title>
    <style>
        #result{
            width: 200px;
            height: 100px;
            border: solid 1px #258;
        }
    </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();
            //超时设置2s
            xhr.timeout = 2000;
            //超时回调
            xhr.ontimeout =function(){
                alert("网络异常,请稍后重试!");
            }
            //网络异常回调
            xhr.onerror = function(){
                alert("你的网络似乎出了一些问题!");
            }
            xhr.open("GET",'http://127.0.0.1:8080/delay');
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    if(xhr.status >= 200 && xhr.status<300){
                        result.innerHTML = xhr.response;
                    }
                }
            }
        })

    </script>
</body>
</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>请求问题</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:8080/delay');
            x.send();
        }

        //abort取消请求
        btns[1].onclick = function(){
            x.abort();
        }
        
    </script>
</body>
</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>重复请求问题</title>
</head>
<body>
    <button>发送请求</button>

    <script>
        const btns = document.querySelectorAll('button');
        let n = null;
        //标识变量
        let isSending = false;//是否正在发送AJAX请求

        btns[0].onclick = function(){
            if(isSending) n.abort();
            
            n = new XMLHttpRequest();
            //修改 标识变量
            isSending = true;
            n.open("GET",'http://127.0.0.1:8080/delay');
            n.send();
            n.onreadystatechange = function(){
                if(n.readyState === 4){
                    //修改标识变量
                    isSending = false;
                }
            }
        }


    </script>
</body>
</html>
上一篇:如何学习机器学习 (Definition of ML)


下一篇:性能分析之IO分析-从IO高到具体文件