尚硅谷Ajax笔记


b站链接

介绍

ajax优缺点
在这里插入图片描述
http

node.js下载配置好环境

express框架
切换到项目文件夹,执行下面两条命令
有报错,退出用管理员身份打开
或者再命令提示符用管理员身份打开

npm init --yes
npm i  express

请求

    <script>
        //引入express
        const express = require('express');
        //创建应用对象
        const app = express();

        //创建路由规则
        //request对请求报文的封装
        //response是对响应报文的封装
        app.get('/',(request,response)=>{
            //设置响应
            response.send('HELLO EXPRESS');
        });

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

ajax请求

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</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(){
        // console.log('test');
        //创建对象
        const xhr = new XMLHttpRequest();
        const result = document.getElementById("result");
        //初始化 设置请求方法和url
        xhr.open('GET','http://127.0.0.1:8000/server');
        //发送
        xhr.send();
        xhr.onreadystatechange = function(){
            //判断(服务端返回了所有的结果)
            if(xhr.readyState === 4){
                if(xhr.status >=200 && xhr.status<300){
                    //响应行
                    /* console.log(xhr.status);//状态码
                    console.log(xhr.statusTest);//状态字符串
                    console.log(xhr.getAllResponseHeaders());//所有响应头
                    console.log(xhr.response); */

                    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>Document</title>
    <style>
        #result{
            width: 200px;
            height: 100px;
            border: solid 1px #90b;
        }
    </style>
</head>
<body>
    <div id="result"></div>
    <script>
        const result = document.getElementById("result");
        //绑定事件
        result.addEventListener("mouseover",function(){
            //创建对象
            const xhr = new XMLHttpRequest();
            //初始化 设置类型与URL
            xhr.open('POST','http://127.0.0.1:8000/server');
            //发送
            xhr.send('1234567');
            //事件绑定
            xhr.onreadystatechange = function(){
                //判断
                if(xhr.readyState === 4){
                    if(xhr.status>=200 && xhr.status<300){
                        //处理服务端返回结果
                        result.innerHTML=xhr.response;
                    }
                }
            }
        })
    </script>
</body>
</html>
//引入express
const express = require('express');
//创建应用对象
const app = express();

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

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

设置请求头

//设置请求头
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
              xhr.setRequestHeader('name','atguigu');
            //发送
            xhr.send('a=100 & b=200 &c=300');

在这里插入图片描述

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

json数据响应

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #result{
            width: 200px;
            height: 100px;
            border: solid 1px #90b;
        }
    </style>
</head>
<body>
    <div id="result"></div>
    <script>
        const result = document.getElementById("result");
        //绑定事件
        window.onkeydown = function(){
       
            //创建对象
            const xhr = new XMLHttpRequest();
            //设置响应体数据类型
            xhr.responseType = 'json';
            //初始化 设置类型与URL
            xhr.open('GET','http://127.0.0.1:8000/json-server');
            //设置请求头
            xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhr.setRequestHeader('name','atguigu');
            //发送
            xhr.send();
            //事件绑定
            xhr.onreadystatechange = function(){
                //判断
                if(xhr.readyState === 4){
                    if(xhr.status>=200 && xhr.status<300){
                        console.log(xhe.response);
                        //处理服务端返回结果
                        result.innerHTML=xhr.response.name;
                    }
                }
            }
        }
    </script>
</body>
</html>
//引入express
const express = require('express');
//创建应用对象
const app = express();

//创建路由规则
//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-Hearders','*')
    //响应一个数据
    const data = {
        name:'atguigu'
    };
    //对对象进行字符串转换
    let str = JSON.stringify(data);
    //设置响应体
    response.send(str);
});

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

nodemon
有报错,退出软件用管理员身份打开

npm install -g nodemon
nodemon server.js

ie缓存

<script>
        const btn = document.getElementsByTagName('button')[0];
        const result = document.querySelector('#result');

        btn.addEventListener('click',function(){
            // console.log('test');
            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>
//引入express
const express = require('express');
//创建应用对象
const app = express();

//创建路由规则
//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-Hearders','*')
    //响应一个数据
    const data = {
        name:'atguigu'
    };
    //对对象进行字符串转换
    let str = JSON.stringify(data);
    //设置响应体
    response.send(str);
});

app.all('/json-server',(request,response)=>{
    //设置响应头  设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');
    //响应头
    response.setHeader('Access-Control-Allow-Hearders','*')
    //响应一个数据
    const data = {
        name:'atguigu'
    };
    //对对象进行字符串转换
    let str = JSON.stringify(data);
    //设置响应体
    response.send(str);
});

app.get('/ie',(request,response)=>{
    //设置响应头  设置允许跨域
    response.setHeader('Access-Control-Allow-Origin','*');
    //设置响应体
    response.send('HELLO IE');
});
//监听端口启动服务
app.listen(8000,()=>{
    console.log("服务已经启动,8000端口监听中……");
});

超时与网络异常

 <script>
        const btn = document.getElementsByTagName('button')[0];
        const result = document.querySelector('#result');

        btn.addEventListener('click',function(){
            // console.log('test');
            const xhr = new XMLHttpRequest();

            //超时
            xhr.timeout = 2000;
            //超时回调
            xhr.ontimeout = function(){
                alert("网络异常,请稍后重试")
            }
            //网络异常回调
            xhr.onerror = function(){
                alert("你的网络出现了问题")
            }
            xhr.open("GET",'http://127.0.0.1:8000/delay');
            xhr.send();
            xhr.onreadystatechange = function(){
                if(xhr.readyState === 4){
                    if(xhr.status >=200 && xhr.status<300){
                        result.innerHTML = xhr.response;
                    }
                }
            }
        })
    </script>

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

取消请求


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <button>点击登录</button>
    <button>点击取消</button>
    <script>
        const btns = document.querySelectorAll('button');
        let x =null;

        btns[0].onlick = function(){
            x=new XMLHttpRequest();
            x.open("GET",'http://127.0.0.1:8000/delay');
            x.send();
        }

        btns[1].onlick = function(){
            x.abort();
        }
    </script>
</body>
</html>

请求重复发送问题

<script>
        const btns = document.querySelectorAll('button');
        let x =null;

        btns[0].onlick = function(){
            //判断标识变量
            if(isSending) x.abort();//如果正在发送,则取消该请求,创建一个新请求
            x=new XMLHttpRequest();
            isSending = true;
            x.open("GET",'http://127.0.0.1:8000/delay');
            x.send();
            x.onreadystatechange=function(){
                if(x.readyState === 4){
                    isSending = false;
                }
            }
        }

        btns[1].onlick = function(){
            x.abort();
        }
    </script>

怎么感觉学的迷迷糊糊的……
emo中

我还会回来的……

二级目录

三级目录

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 尚硅谷的Vue2笔记是学习Vue技术的好帮手。这份笔记详细地介绍了Vue的基本概念和使用方法,包括Vue的属性、指令、事件、计算属性、过滤器、组件等等。通过阅读这份笔记,我们可以了解Vue的整个生命周期,并且学习如何在Vue中绑定数据、响应事件、使用组件化等等。另外,笔记中也提到了Vue的MVVM模式、路由、状态管理、Ajax等进阶使用方法,以及Vue的一些注意点和优化技巧,这些非常实用且有助于深入学习和应用Vue技术。 总体来说,尚硅谷的Vue2笔记内容丰富、清晰易懂,适合初学者和中级开发者使用,是学习Vue技术的一份不错资料。需要注意的是,笔记中的代码及部分内容可能存在过时和不准确的情况,需要和Vue官方文档及其他权威资料进行比较和验证。 ### 回答2: 尚硅谷的Vue2笔记是一份非常全面和深入的Vue学习资料,它涵盖了Vue框架的基本概念和重要特性,包括Vue的组件化开发、指令、路由、Vuex状态管理、axios网络请求等。该笔记不仅注重理论知识的讲解,而且注重实战应用。它提供了大量的示例代码和练习项目,帮助学习者理解和掌握Vue的核心概念和技术。 在Vue2笔记中,作者从Vue的基本概念和MVVM架构模式开始讲解,然后逐步深入介绍了Vue的各种特性和用法,如组件、生命周期、计算属性、watch、事件处理、槽位、指令等等。特别是在组件化开发方面,作者详细介绍了组件之间的通信方式、props和$emit的使用、slot插槽的应用等等,这些都是Vue组件化开发中非常重要的概念。 除了组件化开发之外,Vue2笔记还详细介绍了Vue的路由、状态管理和网络请求等其他关键特性。在路由方面,作者讲解了Vue-Router的基本使用和路由守卫的应用,让学习者能够掌握Vue应用的页面导航和权限控制。在状态管理方面,作者讲解了Vuex的设计思想和使用方法,使学习者能够在复杂的应用中更好地管理和共享状态。在网络请求方面,作者介绍了axios库的使用和封装方法,帮助学习者更好地理解Vue应用中的数据请求和展示。 总的来说,尚硅谷Vue2笔记对于学习Vue框架的人来说是一份非常优秀的教材。它详细介绍了Vue的各个方面,并提供了丰富的练习项目,帮助学习者更好地掌握Vue的核心技术和应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

佳美不ERROR

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

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

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

打赏作者

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

抵扣说明:

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

余额充值