zoukankan      html  css  js  c++  java
  • 把ajax封装成类,用着方便

      一直以来喜欢使用AJAX做一些方便的页面小功能,但是每次都写教案觉很费劲,于是封装了个简单的ajax类。毕竟不是精通js,还望各位指点一二~~
      不废话了,贴代码~~
    function ajax()
    {
        this.request;
        this.header="/home/";//ajax初学最容易遇到的问题之一,路径问题,,一定要从域名后就开始写~~不是相对路径也不是绝对路径哈
        this.createRequest=function()
        {
            var requests;
            if(window.XMLHttpRequest)
            {
                requests=new XMLHttpRequest();
            }
            else
            {
                requests=new ActiveXObject("Microsoft.XMLHTTP");
            }
            return requests;
        }
        this.send=function(lujing,val,huidiao)//第一个是路径,要和上面定义的路径头接上;第二个是要post的数据,object数据类型;第三个是回调函数
        {
            that=this;
            this.request=this.createRequest();
            this.request.open("POST",this.header+lujing+"?id="+Math.random(),true);
            this.request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
            this.request.onreadystatechange=function(){
                huidiao(that.request);//把request作为参数传入回掉函数,不用再放全局变量了
            }
            var txt=this.objjie(val);//将object对象组装成字符串,供send方法使用
            this.request.send(txt);
        }
        this.objjie=function(val)//传入val为对象,其内仅有键值对,无其他复合类型
        {
            var txt="";
            for(var i in val)
            {
                txt+=i+"="+val[i]+"&";
            }
            txt=txt.substring(0,txt.length-1);
            return txt;
        }
    }
  • 相关阅读:
    业务对象(BO)设计
    业务对象和BAPI
    LSMW应用
    BDC、CATT批量数据维护
    ABAP RFC远程调用
    LIST动态表格画线(动态列)
    ALV详解:OO SALV
    ALV详解:OO ALV
    ALV详解:Function ALV(二)
    ALV详解:Function ALV(一)
  • 原文地址:https://www.cnblogs.com/gaokaitai/p/4979356.html
Copyright © 2011-2022 走看看