• 原生ajax的get和post方法封装


    get 方法

        function serialize (data) {
          if (!data) {
            return '';
          }
          
          var paris = [];
          for (var key in data) {
            if (!data.hasOwnProperty(key) || typeof data[key] === 'function') {
              continue;
            }
            var name = encodeURIComponent(key);
            var value = encodeURIComponent(data[key].toString());
            paris.push(name + '=' + value);
          }
          return paris.join('&');
        }
    
        function get (url, options, callback) {
          var req;
          if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
          } else if (window.ActiveXObject) { // 兼容IE7及以下版本
            req = new ActiveXObject();
          }
          
          req.onreadystatechange = function () {
            if (req.readyState === 4) {
              if (req.status === 200) {
                console.log('请求成功');
                callback(req.response);
              }
            } else {
              console.log('请求中...');
            }
          }
          
          // 将传递的参数序列化
          if (serialize(options) !== '') {
            url = url + '?' + serialize(options);
          }
    
          req.open('get', url);
          req.send(null);
        }

     post方法

        function serialize (data) {
          if (!data) {
            return '';
          }
          
          var paris = [];
          for (var key in data) {
            if (!data.hasOwnProperty(key) || typeof data[key] === 'function') {
              continue;
            }
            var name = encodeURIComponent(key);
            var value = encodeURIComponent(data[key].toString());
            paris.push(name + '=' + value);
          }
          return paris.join('&');
        }
    
        function post (url, options, callback) {
          var req;
          if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
          } else if (window.ActiveXObject) { // 兼容IE7及以下版本
            req = new ActiveXObject();
          }
          
          req.onreadystatechange = function () {
            if (req.readyState === 4) {
              if (req.status === 200) {
                console.log('请求成功');
                callback(req.response);
              }
            } else {
              console.log('请求中...');
            }
          }
    
          req.open('post', url);
          req.send(serialize(options));
        }

    get与post方法结合

        function serialize (data) {
          if (!data) {
            return '';
          }
          
          var paris = [];
          for (var key in data) {
            if (!data.hasOwnProperty(key) || typeof data[key] === 'function') {
              continue;
            }
            var name = encodeURIComponent(key);
            var value = encodeURIComponent(data[key].toString());
            paris.push(name + '=' + value);
          }
          return paris.join('&');
        }
    
        function request (method, url, options, callback) {
          var req;
          if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
          } else if (window.ActiveXObject) { // 兼容IE7及以下版本
            req = new ActiveXObject();
          }
          
          req.onreadystatechange = function () {
            if (req.readyState === 4) {
              if (req.status === 200) {
                console.log('请求成功');
                callback(req.response);
              }
            } else {
              console.log('请求中...');
            }
          }
          
          url = method === 'get' && serialize(options) !== '' ?  url + '?' + serialize(options) : url;
          let sendParams = method === 'get' ? null : serialize(options);
          
          req.open(method, url);
          req.send(sendParams);
        }
  • 相关阅读:
    学习一波cmd
    青春,就是用來懷念的
    菜鸟的 linux 学习笔记 -- OOM
    python 获取本机 IP
    win8防火墙配置出站规则禁止QQ访问
    netsh配置Windows防火墙(advfirewall)
    TCP/IP协议
    TCP的状态 (SYN, FIN, ACK, PSH, RST, URG)
    Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
    Photoshop  cs6 快捷键命令大全
  • 原文地址:https://www.cnblogs.com/qiuxiaozhen/p/10568314.html
Copyright © 2020-2023  润新知