• 重写了下Ajax请求Webservice,顺便复习一下Javascript的闭包概念


    var AjaxRequest = function(){
        //返回处理结果的回调函数
        this.agentCallBack = {};
        //javascript 调用domino代理的方法。
        this.ajaxCallAgent = function(agentParam){
            $.ajax({
            type: "POST",
            url: agentParam.url ,
            data: agentParam.jsondata,
            beforeSend: function(request) {
                 //request.setRequestHeader("User-Agent","Apache-HttpClient/4.1.1 (java 1.5)");
            },
            success: agentParam.agentCallBack,
            error: function(request, errorInfo) {
              alert("agent call failed, please contact your administrator.",errorInfo); // alert("errorInfo = "+errorInfo);
            }
          });
        };
    
        //返回处理结果的回调函数
        this.serviceCallBack = {};
        //调用javascriptWebService方法
        this.CallWebService =function (serviceParam){
            this.serviceCallBack = serviceParam.callBack;
          $.ajax({
            type: "POST",
            url: serviceParam.url ,
            data: serviceParam.soapdata,
            beforeSend: function(request) {
              request.setRequestHeader("Content-Type", "text/xml; charset=gbk"); //
              request.setRequestHeader("SOAPAction", serviceParam.soapaction);
            },
            success: this.serviceCallBack,
            error: function(request, errorInfo) {
              alert("invoke webservice failed, please contact your administrator.");
            }
          });
        }
    
        return this;
    }
    
    /*
    * 调用webservice的参数,目的是在重复调用时,一次初始化固定的数据
    * 变化的参数在调用前初始化不变的默认初始化就可以了
    */
    var serviceParam =function(){
        this.dbPath = $("[name=dbPath]").val();
        this.url = "/"+this.dbPath+ "/wsPurchaseOrder?OpenWebService";            //默认初始化参数
        this.ordernumber = $("[name=fldCgddbh]").val();
        this.soapaction = {};                                //调用前初始化, 需要new 一个参数对象
        this.callback = {};
        this.soapdata = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:DefaultNamespace">'+
                   '<soapenv:Header/><soapenv:Body><urn:orderNumber>'+this.ordernumber+'</urn:orderNumber></soapenv:Body></soapenv:Envelope>';
        return this;
    };
    
    /*
    * 调用代理参数少, 没有action
    * 传递参数
    */
    var agentParam =function(){
        this.dbPath = $("[name=dbPath]").val();
        this.url = "/"+this.dbPath+ "/agent?openagent";                            //默认初始化参数                                                
        this.callback = {};                                        //调用前初始化, 需要new 一个参数对象
        this.jsondata = {};                                        //例如:{ordernumber:ordernumber}
        return this;
    };

     可以拷贝所有代码另存为AjaxRequest.js后引入项目使用

  • 相关阅读:
    Java回顾之Spring基础
    Java回顾之ORM框架
    Java回顾之JDBC
    Java回顾之一些基础概念
    Java回顾之反射
    Java回顾之序列化
    platform_device与platform_driver
    DB9 公头母头引脚定义及连接
    浅谈UML的概念和模型之UML九种图
    为Windows 7的winsxs目录瘦身,谨慎。
  • 原文地址:https://www.cnblogs.com/dangkei/p/11943554.html
Copyright © 2020-2023  润新知