AJAX基本操作

设置请求参数

//1. 引入express
const express = require('express')

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

//3.创建路由规则
//request是对请求报文的封装
//response是对相应报文的封装
app.get('/server',(request,response)=>{
    //设置响应头  设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');

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

    //设置响应体
    response.send('Hello Ajax');
});

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

发送get请求

<!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];
        btn.onclick=function(){
            //1.创建对象
            const xhr = new XMLHttpRequest();

            //2.初始化,设置请求方法和URL
            xhr.open('GET','http://127.0.0.1:8000/server?a=100&b=200&c=300');

            //3.发送
            xhr.send();

            //4.事件绑定,处理服务端返回的结果
            //on  在此处有when的意思,当...时候
            //readystate 是xhr对象当中的属性,表示状态 0 1 2 3 4
            //change  改变的时候触发,会触发四次  0->1,1->2,2->3,3->4
            xhr.onreadystatechange = function(){
                //判断(服务端返回了所有结果)
                if(xhr.readyState == 4){
                    //判断响应状态码 200,404,403,401,500
                    //2xx成功
                    if(xhr.status >= 200 && xhr.status <300){
                        //处理服务端响应结果 行 头 空行 体
                        //响应行
                        /* console.log(xhr.status);    //响应状态码
                        console.log(xhr.statusText);  //状态字符串
                        console.log(xhr.getAllResponseHeaders); //所有响应头
                        console.log(xhr.response); */

                        //设置result的文本
                        result.innerHTML = xhr.response;

                    } else {
                        
                    }
                }
            }

        }
    </script>
</body>
</html>

发送post请求

<!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(){
            //1.创建对象
            const xhr = new XMLHttpRequest();

            //2.初始化,设置类型与URL
            xhr.open('POST','http://127.0.0.1:8000/server');

            //3.发送
            xhr.send();

            //4.事件绑定
            xhr.onreadystatechange = function(){
                //判断
                if(xhr.readyState == 4){
                    if(xhr.status >= 200 && xhr.status < 300){
                        //处理服务端返回的结果
                        result.innerHTML = xhr.response;

                    }
                }
            }
        });
    </script>
</body>
</html>

服务端响应JSON请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON 响应</title>
    <style>
        #result{
            width: 200px;
            height: 100px;
            border: solid 1px #89b;
        }
    </style>
</head>
<body>
    <div id="result"></div>
    <script>
        const xhr = new XMLHttpRequest();
        //绑定键盘按下事件
        window.onkeydown = function(){
            //发送请求
            const xhr = new XMLHttpRequest();
            //设置响应体数据的类型
            xhr.responseType = 'json';

            //初始化
            xhr.open('GET','http://127.0.0.1:8000/json-server');

            //发送
            xhr.send();

            //事件绑定
            xhr.onreadystatechange = function(){
                if(xhr.readyState == 4){
                    if(xhr.status >= 200 && xhr.status <300){
                        //处理返回结果
                        /* console.log(xhr.response);
                        result.innerHTML = xhr.response; */

                        //手动对数据转换
                        /* let data = JSON.parse(xhr.response);
                        console.log(data);
                        result.innerHTML = data.name; */

                        //自动对数据转换
                        console.log(xhr.response);
                        result.innerHTML = xhr.response.name;
                        
                    }
                }
            }

        }

    </script>
</body>
</html>

IE缓存问题解决方案

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <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();
            xhr.open("GET",'http:127.0.0.1:8000/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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mmk198

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值