zoukankan      html  css  js  c++  java
  • 希望找人一起写个 Ajax 的封装

    我已经写好了一些

    xmlHttp.js
    /**
    * @version 1.0
    * @时间  2008-06-26
    * @作者  林松斌
    * @QQ    410728115
    * @Email linsongbin@126.com
    * @博客 http://linsongbin.cnblogs.com/
    */
    Ajax = function() {
    this._url = null;
    this._method = 'GET';
    this._xmlhttp = null;
    this.createXMLRequest();
    }
    Ajax.prototype.setUrl = function(url) {
        this._url = url;
    }
    Ajax.prototype.setMethod = function(method) {
        this._method = method;
    }
      
    Ajax.prototype.createXMLRequest = function() {
        try {
            this._xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (ex1) {
            try {
                this._xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (ex2) {
                this._xmlhttp = null;
            }
        }
    if (!this._xmlhttp) {
      if (typeof XMLHttpRequest != "undefined") {
       this._xmlhttp = new XMLHttpRequest();
      }
    }
    }

    Ajax.prototype.send = function(url, method, data) {
        this._url = url;
        this._methdo = method;
        var self = this;
        if (this._method.toLowerCase() == "get") {
            this._xmlhttp.open("GET", this._url, true);
            this._xmlhttp.send(null);
        } else if(this._method.toLowerCase() == "post") {
            this._xmlhttp.open("POST", this._url, true);
            this._xmlhttp.setRequestHeader('Content-Type', 'application/ x-www-form-ur lencoded');
            this._xmlhttp.send(data);
        }
    this._xmlhttp.onreadystatechange = function(){self.process.call(self)};  
    }
    //这里需要从写
    Ajax.prototype.process = function() {
    switch (this._xmlhttp.readyState) {
      case 1:
       //this.onLoading();
       break;
      case 2:
       //this.onLoaded();
       break;
      case 3:
       //this.onInteractive();
       break;
      case 4:
          alert(this._xmlhttp.responseText);
    }
    }
    /**
    * 提供简易操作接口
    */
    sendGET = function(url) {
        var ajax = new Ajax();
        ajax.send(url, 'post', null);
    }


    接着就要对 callback 函数进行重构
    时刻记着: 安全, 性能, 易用 !!
    如果有兴趣请与我联系!!
  • 相关阅读:
    duilib框架分析:几个回调(C++11)
    duilib框架分析(一)概述
    图解JVM--(二)垃圾回收
    图解jvm--(一)jvm内存结构
    4 (计算机网络) DHCP与PXE:IP是怎么来的,又是怎么没的?
    3(计算机网络)ifconfig:最熟悉又陌生的命令行
    2 (计算机网络)理解网络协议的工作模式
    1 (计算机网络)我们常用的网络协议有哪些?
    阿里云配置mysql
    深入Spring Boot:那些注入不了的Spring占位符(${}表达式)
  • 原文地址:https://www.cnblogs.com/LinFx/p/2123702.html
Copyright © 2011-2022 走看看