jQuery里面ajax请求的封装
为了避免ajax漫天飞,我们需要对jQuery的代码进行封装,封装代码:
function api_request(name, params, cb, scope, async, el) {
if (async == null)
async = true;
console.log('调用接口:\n%s,\n参数列表:', REQUEST_URL+name, params);
$.ajax( {
url : REQUEST_URL+name,
async : async,
data : params,
type : 'POST',
dataType:'json',
cache : false,
timeout:70000,
success : function(data, textStatus) {
//alert(data.obj[0].id);
if (data.resultCode == 0001) {
window.location.href = 'common/index.jsp?' + $.param( {
to : window.location.href});
return;
}
if (data.resultCode != 0000 && data.resultCode != 0007) {
}
cb.call(scope || window, data, textStatus);
},
error:function(xhr){
alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
}
});
};
请求方式:
api_request('../../' + HB_SPECIAL_NUM_DEL, param, cb, null, true, null);
加载数据:
//加载数据
function cb(data, textStatus) {
if(data.status){
console.log(data.obj);
data.obj = JSON.parse(data.obj);
console.log(data.obj);
var content = data.obj.content;
if (content.length > 0) {
for (var i = 0; i < content.length; i++) {
h = "<tr>";
/*h += '<td class="center"><label class="pos-rel"><input type="radio" class="ace" name="id" value="' + content[i].id + '"><span class="lbl"></span></label></td>';*/
h += "<td>" + content[i].tel + "</td>";
h += "<td>" + (content[i].areaNo && content[i].areaNo != '-1'?content[i].areaNo:'') + "</td>";
h += "<td>" + (content[i].rangeType==1?'区域':'业务') + "</td>";
h += "<td>" + content[i].productId + "</td>";
h += "<td>" + (content[i].createTime?common.longTimeFormat(content[i].createTime):'') + "</td>";
h += "<td>" + (content[i].source?getSource(content[i].source):'') + "</td>";
h += "<td>" + getMemo(content[i].memo) + "</td>";
h += "<td>" + (content[i].nodeCode?content[i].nodeCode:'') + "</td>";
h += "<td>" + (content[i].adminNamel?content[i].adminNamel:'') + "</td>";
h += "<td ><div class='btn-group'>" +
"<a href='JavaScript:void(0);' style='color:#FFFFFF;' " +
"onclick='del(\""+content[i].id+"\");'>" +
"<button class='btn btn-xs btn-danger border-radius'>" +
"<i class='ace-icon fa fa-trash-o'>" +
" 删除</i></button></a>" +
"</div></td>";
h += "</tr>";
$html.append(h);
} /*$("#roleListPage").pagination(data.total, {
callback: pageselectCallback,
prev_text: '<上一页',
next_text: '下一页 >',
items_per_page: size,
num_display_entries: 6,
current_page: page,
num_edge_entries: 2
});*/
} else {
h = "<tr class='center'><td colspan='10'>暂无数据</td></tr>";
$html.append(h);
}
}else{
h = "<tr class='center'><td colspan='10'>"+data.desc+"</td></tr>";
$html.append(h);
}
}
jQuery里面ajax请求的封装的更多相关文章
- 使用es6的then()方法封装jquery的ajax请求
使用场景: jsp页面中使用jquery的ajax请求比较频繁,以前vue框架的项目用过axios,所以就想着用then()封装一个公共请求的方法,这样每次请求就不用那么麻烦的写一大堆请求参数了. 示 ...
- jQuery发送Ajax请求以及出现的问题
普通jQuery的Ajax请求代码如下: $.ajax({ type: 'POST', url: "http://xxx/yyy/zzz/sendVerifyCode", data ...
- Sping MVC不使用任何注解处理(jQuery)Ajax请求(基于XML配置)
1. Spring Spring框架是一个轻量级的解决方案,是一个潜在的一站式商店,用于构建企业就绪的应用程序.Spring框架是一个Java平台,为开发Java应用程序提供全面的基础架构支持.Spr ...
- Struts2处理(jQuery)Ajax请求
1. Ajax Ajax(Asynchronous JavaScript and XML,异步JavaScript和XML)时一种创建交互式网页应用的网页开发技术,它并不是一项新的技术,其产生 ...
- Servlet处理(jQuery)Ajax请求
1. jQuery jQuery是一个JavaScript函数库,极大的简化了JavaScript编程,很容易学习.jQuery是目前最流行的开源js框架,并且提供了大量的扩展. 2. Aja ...
- 学习AJAX必知必会(4)~JQuery发送Ajax请求
一.JQuery发送Ajax请求 ■ 对于get和post请求,jQuery内部封装了Ajax请求的4个步骤和数据格式的设置 ■ 对于Ajax通用请求,jQuery内部封装了Ajax请求的4个步骤和数 ...
- jQuery发送ajax请求
利用jquery发送ajax请求的几个模板代码. $.ajax({ async : false, type: 'POST', dataType : "json", url: &qu ...
- JQuery发送ajax请求不能用数组作为参数
JQuery发送ajax请求不能用数组作为参数,否则会接收不到参数, 一.js代码如下: $('#delete-button').click(function(){ var select ...
- Angular和jQuery的ajax请求的差别
近期项目中使用angular,结果发现后台没法获取參数,所以,略微研究了一下两者在发送ajax时的差别. 注意angular和jquery的ajax请求是不同的. 在jquery中,官方文档解释con ...
随机推荐
- 深入理解Java虚拟机(一)
一.运行时数据区域 1.程序计数器: 当前线程执行字节码的行号指示器(通过改变计数器的值来选择下条需要执行的字节码指令) 每个线程有独立的程序计数器(线程私有,为了切换线程时能恢复到挣钱的执行位置 ...
- unity, WaterProDaytime注意事项。
一,多个WaterProDaytime不要公用material. 原因是:水面material的shader(FXWaterPro.shader)引用了reflectionTexture,而水面1的r ...
- 图像中的artifacts
artifacts 瑕疵 伪影(Artifacts) 伪影(Artifacts)-CT-基础术语 - 影像园 http://www.xctmr.com/baike/ct/c34b5413e305b45 ...
- python缓存装饰器,第二种方式(二)
来个简单的装饰器 def cached_method_result(fun): """方法的结果缓存装饰器""" @wraps(fun) d ...
- DB索引、索引覆盖、索引优化
###########索引########### @see http://mp.weixin.qq.com/s/4W4iVOZHdMglk0F_Ikao7A 聚集索引(clustered inde ...
- org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'parentId' in 'class java.lang.String'
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ' ...
- SpringMVC由浅入深day02_8json数据交互
8 json数据交互 8.1 为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据 ...
- 一张图了解SSH端口转发
ssh和端口转发什么的,我就不想废话了,主要是ssh的命令格式真心不太好理解.网上也搜过相关文章,参差不齐.其实自己也理解怎么用,但我自己也表达不好.这几日无意碰到篇好文章,有图有真相,清楚的很,还有 ...
- iOS开发--UIButton 设置圆角 边框颜色 点击回调方法
UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom]; signBtn.frame = CGRectMake(, , , ) ...
- Splash runjs() 方法
runjs() 方法可以执行 JavaScript 代码,它与 evaljs() 功能类似,但是更偏向于执行某些动作或声明某些方法 function main(splash, args) splash ...