首页 > 其他分享 >XHR 的事件与FormData

XHR 的事件与FormData

时间:2023-03-24 09:56:57浏览次数:51  
标签:status const log xhr url FormData XHR 事件 console

XHR 的事件

  load事件

    

    //1.load事件
            //响应数据可用时触发        
           const url ='https://www.imooc.com/api/http/search/suggest?words=js';
                const xhr = new XMLHttpRequest();
               // xhr.onload = () => {
               //         /* if (xhr.readyState != 4) return; */
               //          if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
               //          console.log(xhr.response);
               //          }
               //      };
                 
                 xhr.addEventListener('load', () => {
                     if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
                     console.log(xhr.response);
                     }
                 },false);
                 
                    xhr.open('GET', url, true);
                   
                    xhr.send(null);    
                                                 
                //IE6~8不支持load事件                        

 

  erro事件

  

 

   

 

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script>
            //2.error事件
            //请求发生错误的时触发    
            //const url ='https://www.imooc.com/api/http/search/suggest?words=js';
            const url = 'https://www.iimooc.com/api/http/search/suggest?words=js';
            
            
            const xhr = new XMLHttpRequest();
            xhr.addEventListener('load', () => {
                if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
                    console.log(xhr.response);
                }
            }, false);
            
            xhr.addEventListener('error',()=>{
                console.log('error');
            },false)
            
            xhr.open('GET', url, true);
            
            xhr.send(null);
            //IE10开始支持
        </script>
    </body>
</html>

  abort事件

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
        <script>
            //3.abort事件
            //调用abort()终止请求时触发
            const url ='https://www.imooc.com/api/http/search/suggest?words=js';
            const xhr = new XMLHttpRequest();
            xhr.addEventListener('load', () => {
                if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
                    console.log(xhr.response);
                }
            }, false);
            
            xhr.addEventListener('abort',()=>{
                console.log('abort');
            },false);
            
            xhr.open('GET', url, true);
            
            xhr.send(null);    
            
            xhr.abort();    
            
            //IE10开始支持    
        </script>
    </body>
</html>

 

 

 

  timeout事件

 

 

FormData

  使用Ajax提交表单

      

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>FormData</title>
    </head>
    <body>
        <form id="login" action="https://www.imooc.com/api/http/search/suggest?words=js" method="post"
            enctype="application/x-www-form-urlencoded">
            <input type="text" name="username" placeholder="用户名" />
            <input type="password" name="password" placeholder="密码" />
            <input id="submit" type="submit" value="提交" />
        </form>
        <script>
            //1.使用Ajax提交表单
            
            //2.FormDate的基本用法
            //通过HTML表单元素创建FormDate对象
            //const fd = new FormData(表单元素);
            //data.append('age',18);
            //data.append('sex','male');
            //xhr.send(fd);
            
            //通过append()方法添加数据    
            
            const login = document.getElementById('login');
            //console.log(login.username);
            //console.log(login.password);
            const {
                username,
                password
            } = login;
            const btn = document.getElementById('submit');

            const url = 'https://www.imooc.com/api/http/search/suggest?words=js'

            btn.addEventListener(
                'click',
                e => {
                    //阻止表单自动提交
                    e.preventDefault();
                    //表单数据验证

                    //发生Ajax请求
                    const url = 'https://www.imooc.com/api/http/search/suggest?words=js';
                    const xhr = new XMLHttpRequest();
                    xhr.addEventListener('load', () => {
                            if ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304) {
                                console.log(xhr.response);
                            }
                        },
                        false
                    );
                    xhr.open('POST', url, true);

                    //组装数据
                    //const data = 'username=${username.value}&password=${passwrod.value}';
                    
                    const data = new FormData(login);
                    data.append('age',18);
                    data.append('sex','male');
                    //console.log(data);
                    /* for(const item of data){
                        console.log(item);
                    } */
                    // xhr.setRequestHeader(
                    //     'Content-Type',
                    //     'application/x-www-form-urlencoded'
                    // );

                    xhr.send(data);

                    //xhr.send('username=lyw&passwrod=12345')
                },
                false
            );
        </script>
    </body>
</html>

 

      

      

      

 

 

 

   FormData的基本用法

标签:status,const,log,xhr,url,FormData,XHR,事件,console
From: https://www.cnblogs.com/x3449/p/17249470.html

相关文章

  • 产品快讯丨神策营销云业务事件触发能力
    神策数据:神策数据升级触发型营销能力——以业务事件属性变更作为触发条件,并关联相关用户。例如,在商品降价时,给加购的用户发送提醒「亲爱的李小姐,您关注的XXX降价啦,快来看......
  • XHR 的属性与XHR 的方法
    XHR的属性responseType和response属性<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>responseType和response属性</title>......
  • SAP ABAP OOALV F4事件/回车事件的注册和使用
    *----------------------------------------------------------------------** DEFINE LOCAL CLASS*---------------------------------------------------------------......
  • 家用热水器用户行为分析与事件识别
    1.探索分析热水器的水流量状况#-*-coding:utf-8-*-"""CreatedonMonMar2015:12:412023@author:admin"""importpandasaspdimportmatplotlib.pyplot......
  • OpenLayers change 事件获取当前值
    这里有个change:resolution事件但是事件的内容没有value,只有oldValueZc {type:'change:resolution',target:F,key:'resolution',oldValue:9786.603971778399......
  • threejs之检测拖动模型和停止拖动模型以及鼠标缩放事件
       controls.value.addEventListener('start',startEvent) controls.value.addEventListener('end',endEvent); 这样子给轨道控制器绑定事件后即可。......
  • Magento事件与事件监听
    事件和事件监听也许是magento中最有趣的功能之一,因为它允许开发者来扩展magento应用程序中的关键部分。为了为不同模块之间提供更多的灵活性和便利,magento使用了一种事件/......
  • HTML5中的服务器发送事件Server-sent events
    HTML5加了许多新功能,其中服务器发送事件Server-sentevents是一个亮点,以下直接贴上代码示例 客户端a.html页面<!DOCTYPEhtml><html><head><metacharset......
  • Android中设置RecyclerView的每一个item的单项点击事件
    在上面的基础上怎样设置RecyclerView的每一项的点击事件。注:关注公众号霸道的程序猿获取编程相关电子书、教程推送与免费下载。实现找到RecyclerView对应的适配器Adapter......
  • Python中事件驱动
    提示:如果要回顾视频,直接从第8分钟开始看事件驱动可以说是一个概念,还是用一个官方的语言来讲解一下吧事件驱动专业的解释是指在持续事务管理中,进行决策的一......