zoukankan      html  css  js  c++  java
  • 原生ajax写法

    转自:https://blog.csdn.net/qq_empire/article/details/81737394

    相关链接:https://blog.csdn.net/qq_30101879/article/details/77916622

    原生ajax使用:

       function ajax(url){
            var xhr = window.XMLHttpRequest ? new XMLHttpRequest() : ActiveXObject("microsoft.XMLHttp")
            xhr.open("get",url,true);
            xhr.send();
            xhr.onreadysattechange = () =>{
                if(xhr.readystate == 4){
                    if(xhr.status == 200){
                        var data = xhr.responseTEXT;
                        return data;
                    }
                }
            }    
        }
     

    get方式

       function btnClick() {
            //创建核心对象
            xhr= null;
            if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
                xhr = new XMLHttpRequest();
            } else if (window.ActiveXObject) {// code for IE6, IE5
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            //编写回调函数
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert(xhr.responseText)
                }
            }
            //open设置请求方式和请求路径
            xhr.open("get", "/Ajax/ajax2?username=张三");//一个servlet,后面还可以写是否同步
            //send 发送
            xhr.send();
        }

    post

        function btnClick() {
            //创建核心对象
            xhr = null;
            if (window.XMLHttpRequest) {// code for Firefox, Opera, IE7, etc.
                xhr = new XMLHttpRequest();
            } else if (window.ActiveXObject) {// code for IE6, IE5
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
            //编写回调函数
            xhr.onreadystatechange = function() {
                /*     alert(xmlhttp.readyState); */
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert(xhr.responseText)
                }
                /* alert(123); */
            }
            //open设置请求方式和请求路径
            xhr.open("post", "/Ajax/ajax2");//一个servlet,后面还可以写是否同步
            //设置请求头
            xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded")
            //send 发送
            xhr.send("username=张三");
        }

     封装

    function ajax_method(url,data,method,success) {
        // 异步对象
        var ajax = new XMLHttpRequest();
    
        // get 跟post  需要分别写不同的代码
        if (method=='get') {
            // get请求
            if (data) {
                // 如果有值
                url+='?';
                url+=data;
            }else{
    
            }
            // 设置 方法 以及 url
            ajax.open(method,url);
    
            // send即可
            ajax.send();
        }else{
            // post请求
            // post请求 url 是不需要改变
            ajax.open(method,url);
    
            // 需要设置请求报文
            ajax.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    
            // 判断data send发送数据
            if (data) {
                // 如果有值 从send发送
                ajax.send(data);
            }else{
                // 木有值 直接发送即可
                ajax.send();
            }
        }
    
        // 注册事件
        ajax.onreadystatechange = function () {
            // 在事件中 获取数据 并修改界面显示
            if (ajax.readyState==4&&ajax.status==200) {
                // console.log(ajax.responseText);
    
                // 将 数据 让 外面可以使用
                // return ajax.responseText;
    
                // 当 onreadystatechange 调用时 说明 数据回来了
                // ajax.responseText;
    
                // 如果说 外面可以传入一个 function 作为参数 success
                success(ajax.responseText);
            }
        }
    
    }
  • 相关阅读:
    Learning NFS/NIS 2nd 读书笔记-Chapter3 NIS Operation
    Linux Enterprise Cluster Notes Ch11 LVS Introduction Theory
    Linux Enterprise Cluster NOtes Ch7 A Sample HA config
    Linux Enterprise Cluster Notes Ch10 build a Linux cluster
    Linux Enterprise Cluster NOtes Ch8 Heartbeat配置和维护
    当被监控的应用发生问题时,heartbeat会failover么?
    Linux Enterprise Cluster NOtes Ch9 Stonith and IPFail
    Linux Enterprise Cluster NOtes Ch6 Heartbeat介绍和原理
    客户端不支持javascript怎么办
    js 返回对象|js返回多个值的方法|js如何返回多个值
  • 原文地址:https://www.cnblogs.com/llllpzyy/p/10670628.html
Copyright © 2011-2022 走看看