封装ajax方法
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 == ) {
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);
}
})
}
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);
}
封装ajax方法的更多相关文章
- 原生JS封装ajax方法
http://blog.sucaijiayuan.com/article/89 jquery框架的ajax方法固然好用,但是假如某天我们的项目不能引入jquery或项目需求很简单,没有很多交互功能,只 ...
- js jq封装ajax方法
json文本格式 { "userInfo":[ {name:"admin",password:"123"}, {name:"adm ...
- 自已封装Ajax方法
function createXHR() { var request; if (typeof (XMLHttpRequest) == 'undefined') { request = new Acti ...
- Ajax方法封装
打算自己封装一个ajax方法,再不用jq库的情况下,直接引用: ajax作用:数据交互,在不刷新页面的情况下,发送请求,获取数据: 首页第一步常见一个ajax对象:XMLHttpRequest,之后会 ...
- 【前端学习笔记04】JavaScript数据通信Ajax方法封装
//Ajax 方法封装 //设置数据格式 function setData(data){ if(!data){ return ''; } else{ var arr = []; for(k in da ...
- 类似jQuery的原生JS封装的ajax方法
一,前言: 前文,我们介绍了ajax的原理和核心内容,主要讲的是ajax从前端到后端的数据传递的整个过程. Ajax工作原理和原生JS的ajax封装 真正的核心就是这段代码: var xhr = ne ...
- 封装一个类似jquery的ajax方法
//封装一个类似jquery的ajax方法,当传入参数,就可以发送ajax请求 //参数格式如下{ // type:"get"/"post", // dataT ...
- js封装ajax的方法
常用的ajax请求方法封装 /** * ajax请求的封装代码 */ function ajaxPost(url, params, cb) { $.ajax({ type : 'post', url ...
- JQuery封装ajax的方法
1.$.post方法 $.post(url[,data][,callback][,type]) url:请求的后台程序地址 data:发送到后台的数据 callback:载入成功时回调函数,该函数参数 ...
随机推荐
- Codeforces Round #169 (Div. 2) A水 B C区间更新 D 思路
A. Lunch Rush time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Star sky 二维前缀和
C. Star sky time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- Picture POJ - 1177 (线段树-扫描线)
A number of rectangular posters, photographs and other pictures of the same shape are pasted on a wa ...
- SDUT 3930 线段树
皮卡丘的梦想2 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Description 一天,一只住在 501 实验 ...
- phpstorm 安装
16 sudo apt-get install python-software-properties 17 sudo add-apt-repository ppa:webupd8team/java 1 ...
- Python to list users in AWS
code import boto3 c1=boto3.client('iam') #list_users will be a dict users=c1.list_users() #transfer ...
- table第一行合并,其余行宽度失效问题
<col>标签 http://www.w3school.com.cn/tags/tag_col.asp 由于表格中有一行合并过,所以其它没有合并的行的列宽就会平均化,对列宽的设置也会失效. ...
- django自带分页代码
django分页 {% if is_paginated %} <div class="pagination-simple"> <!-- 如果当前页还有上一页,显示 ...
- 【BZOJ1879】【SDOI2009】Bill的挑战 [状压DP]
Bill的挑战 Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Input 第一行:一个整数T, ...
- 【Foreign】咏叹 [模拟退火]
咏叹 Time Limit: 100 Sec Memory Limit: 256 MB Description 有n根木棍,第i根长度为ai.你要贴着墙围出一个矩形区域,木棍围成的矩形边缘必须平行或 ...