zoukankan      html  css  js  c++  java
  • jQuery Ajax封装通用类

    /*****************************************************************
    jQuery Ajax封装(通用)
    *****************************************************************/
    $(function(){
    /**
    * ajax封装
    * url 发送请求的地址
    * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}
    * async 默认值: true。默认设置下,所有请求均为异步请求。如果需要发送同步请求,请将此选项设置为 false。
    * 注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行。
    * type 请求方式("POST" 或 "GET"), 默认为 "GET"
    * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
    * successfn 成功回调函数
    * errorfn 失败回调函数
    */
    jQuery.ax=function(url, data, async, type, dataType, successfn, errorfn) {
    async = (async==null || async=="" || typeof(async)=="undefined")? "true" : async;
    type = (type==null || type=="" || typeof(type)=="undefined")? "post" : type;
    dataType = (dataType==null || dataType=="" || typeof(dataType)=="undefined")? "json" : dataType;
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
    type: type,
    async: async,
    data: data,
    url: url,
    dataType: dataType,
    success: function(d){
    successfn(d);
    },
    error: function(e){
    errorfn(e);
    }
    });
    };
    /**
    * ajax封装
    * url 发送请求的地址
    * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}
    * successfn 成功回调函数
    */
    jQuery.axs=function(url, data, successfn) {
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
    type: "post",
    data: data,
    url: url,
    dataType: "json",
    success: function(d){
    successfn(d);
    }
    });
    };

    /**
    * ajax封装
    * url 发送请求的地址
    * data 发送到服务器的数据,数组存储,如:{"date": new Date().getTime(), "state": 1}
    * dataType 预期服务器返回的数据类型,常用的如:xml、html、json、text
    * successfn 成功回调函数
    * errorfn 失败回调函数
    */
    jQuery.axse=function(url, data, successfn, errorfn) {
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
    type: "post",
    data: data,
    url: url,
    dataType: "json",
    success: function(d){
    successfn(d);
    },
    error: function(e){
    errorfn(e);
    }
    });
    };

    });

  • 相关阅读:
    【转】In ASP.NET using jQuery Uploadify upload attachment
    golang 初体验
    Node.js(express) + MongoDB(mongoose) 简单开发(二)
    Node.js(express) + MongoDB(mongoose) 简单开发(一)
    windows下修改mysql的root密码
    新网站添加百度官方认证
    手写单例模式
    浏览器内核
    IE内核浏览器
    没有猜中开头,更加没有预料结尾的我,正努力走在向程序媛发展的道路上……
  • 原文地址:https://www.cnblogs.com/lxc-bky/p/9531838.html
Copyright © 2011-2022 走看看