jQuery.ajax对应的post/get/delete/put请求方法封装
发表于:2021-12-08 15:02:57浏览:2767次
jQuery ajax中的post/get/delete/put请求方法的写法过于繁琐,所以现在封装成如下简便的形式:
1、获取数据ajax-get请求
/**
* 获取数据ajax-get请求
*/
$.getJSON = function (url,data,callback){
$.ajax({
url:url,
type:"get",
contentType:"application/json",
dataType:"json",
timeout:10000,
data:data,
success:function(data){
callback(data);
}
});
};2、提交json数据的post请求
/**
* 提交json数据的post请求
*/
$.postJSON = function(url,data,callback){
$.ajax({
url:url,
type:"post",
contentType:"application/json",
dataType:"json",
data:data,
timeout:60000,
success:function(msg){
callback(msg);
},
error:function(xhr,textstatus,thrown){
}
});
};3、修改数据的ajax-put请求
/**
* 修改数据的ajax-put请求
*/
$.putJSON = function(url,data,callback){
$.ajax({
url:url,
type:"put",
contentType:"application/json",
dataType:"json",
data:data,
timeout:20000,
success:function(msg){
callback(msg);
},
error:function(xhr,textstatus,thrown){
}
});
};4、删除数据的ajax-delete请求
/**
* 删除数据的ajax-delete请求
*/
$.deleteJSON = function(url,data,callback){
$.ajax({
url:url,
type:"delete",
contentType:"application/json",
dataType:"json",
data:data,
success:function(msg){
callback(msg);
},
error:function(xhr,textstatus,thrown){
}
});
}; 推荐文章
- 前端开发者必须掌握的数据可视化技术
- notepad++ 正则表达式替换常用方法
- PHP将透明图片(PNG)合并到JPG背景图片上,实现PNG透明的效果
- PHP判断网站的访问来源是否是蜘蛛
- php将一个包含父子关系的扁平化数组转换成树形菜单
- bignumber.js,javascript前端高精度计算库推荐
- linux环境下,Composer安装项目时报错:Do not run Composer as root/super user!
- 微软发布首个 Windows 11 ISO 版本 新的浏览器大战又要开始?
- Thinkphp6框架Request类详解
- 阿里云OSS文件上传速度技巧之内网地址上传

