马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

封装ajax方法

[复制链接]
查看3802 | 回复3 | 2017-7-13 09:19:32 | 显示全部楼层 |阅读模式

[C#] 纯文本查看 复制代码
function ajaxRequest(type, url, data, callback, loading, cache) {
    var ajaxConfig = {
        url: '',
        data: {},
        callback: null,
        loading: true,
        cache: true,
        async: true
    };
    // 判断每一个参数url的类型
    // 如果是对象则是请求参数对象
    // 如果是字符串则是请求URL,参数和回调要继续检测后面的参数
    if (typeof url === 'string') {
        ajaxConfig.url = url;
        ajaxConfig.data = data;
        ajaxConfig.callback = callback;
        ajaxConfig.loading = typeof(loading) === 'undefined' ? true : loading;
        ajaxConfig.cache = cache;
    } else {
        ajaxConfig = $.extend({}, ajaxConfig, url);
    }
    $.ajax({
        type: type,
        url: ajaxConfig.url,
        data: ajaxConfig.data,
        beforeSend: function() {
            if (ajaxConfig.loading) { $.showLoading(); }
        },
        dataType: 'json',
        cache: ajaxConfig.cache,
        async: ajaxConfig.async,
        success: function(re) {
            if (ajaxConfig.loading) { $.hideLoading() };
            if (re.result == 1) {
                if (re.msg != '') {
                    $.toast(re.msg, function() {
                        if (re.redirect) {
                            window.location.href = re.redirect;
                        } else {
                            ajaxConfig.callback(re.data);
                        }
                    });
                } else {
                    ajaxConfig.callback(re.data);
                }
            } else {
                $.toast(re.msg, 'forbidden');
            }
        },
        error: function(re) {
            ajaxConfig.loading && $.hideLoading();
            $.toast("Error", "forbidden");
            console.error(re.responseText);
        }
    })
}



调用



[C#] 纯文本查看 复制代码
function ajaxGet(url, data, callback, loading) {
    ajaxRequest('get', url, data, callback, loading, true);
}
function ajaxPost(url, data, callback, loading) {
    ajaxRequest('post', url, data, callback, loading, false);
}

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
ibcadmin | 2017-7-13 09:19:56 | 显示全部楼层
试试
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
allen_ma | 2017-7-19 09:50:26 | 显示全部楼层
站长V587
桂林一枝花 | 2017-9-19 09:32:12 | 显示全部楼层
[JavaScript] 纯文本查看 复制代码
	 	/* 封装ajax函数
	 	 * @param {string}opt.type http连接的方式,包括POST和GET两种方式
	 	 * @param {string}opt.url 发送请求的url
	 	 * @param {boolean}opt.async 是否为异步请求,true为异步的,false为同步的
	 	 * @param {object}opt.data 发送的参数,格式为对象类型
	 	 * @param {function}opt.success ajax发送并接收成功调用的回调函数
	 	 */
 	    function ajax(opt) {
 	        opt = opt || {};
 	        opt.method = opt.method.toUpperCase() || 'POST';
 	        opt.url = opt.url || '';
 	        opt.async = opt.async || true;
 	        opt.data = opt.data || null;
 	        opt.success = opt.success || function () {};
 	        opt.error = opt.error || function(){};
 	        var xmlHttp = null;
 	        if (XMLHttpRequest) {
 	            xmlHttp = new XMLHttpRequest();
 	        }
 	        else {
 	            xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
 	        }
 	        var params = [];
 	        for (var key in opt.data){
 	            params.push(key + '=' + opt.data[key]);
 	        }
 	        var postData = params.join('&');
 	        if (opt.method.toUpperCase() === 'POST') {
 	            xmlHttp.open(opt.method, opt.url, opt.async);
 	            xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=utf-8');
 	            xmlHttp.send(postData);
 	        }
 	        else if (opt.method.toUpperCase() === 'GET') {
 	            xmlHttp.open(opt.method, opt.url + '?' + postData, opt.async);
 	            xmlHttp.send(null);
 	        } 
 	        xmlHttp.onreadystatechange = function () {
 	        	if(xmlHttp.readyState === 4){
 	        		if (xmlHttp.status == 200) {
 	        		    opt.success(xmlHttp.responseText);
 	        		}else{
 	        			opt.error();
 	        		}
 	        	}
 	        };
 	    }

我这个才是棒棒的ajax 封装,不依赖jQ
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则