• 随手封装一个基于Jquery的Ajax API


    (function($){
      var BASE_URL = 'http://120.77.221.25:16850';
      var COMMOR = '/commor/';

      function _validate(args){
        var url = args[0];
        var params = args[1];
        var success = args[2];
        var failed = args[3];
        if(!params instanceof Object){
          throw('params must be a Object')
        }  
        if((success && !success instanceof Function) || (failed && !failed instanceof Function)){
          throw('callback must be a Function')
        }
      }
      function _callback(success,failed){
        return function(res){
          var errcode = res.errcode;
          var data = res.data;
          var errinfo = res.errinfo;
          if(errcode === 0){
            success && success(data)
          }else{
            failed && failed(errcode,errinfo,data)
          }
        }
      }
      function httpGet(url,params,success,failed){
        _validate(arguments);
        $.get(url,params,_callback(success,failed),'json')
      }
      function httpPost(url,params,success,failed){
        _validate(arguments);
        $.post(url,params,_callback(success,failed),'json')
      }
      var commor = {
        sessionid:function(params,success,failed){
        httpGet(BASE_URL + COMMOR +'sessionid',params,success,failed)
      },
      peerid:function(params,success,failed){
        httpPost(BASE_URL + COMMOR +'sessionid',params,success,failed)
      }
      }

      return window.api = {
        commor:commor
      }
    })(jQuery)

    // 调用时
    window.api.commor.sessionid({},function(data){

    },function(errcode,errinfo,data){

    })

  • 相关阅读:
    生成EXCEL报表
    数据库操作
    类似于qq魔法表情的窗口
    关于c#如何遍历listbox里ValueMember属性的值
    ExecuteNonQuery()
    startsWith()
    2019HDU多校第七场 HDU6646 A + B = C 【模拟】
    2019HDU多校第七场 HDU6651 Final Exam
    2019HDU多校第七场 HDU6656 Kejin Player H 【期望递归】
    P1962 斐波那契数列 【矩阵快速幂】
  • 原文地址:https://www.cnblogs.com/qujinwei/p/7055430.html
Copyright © 2020-2023  润新知