封装了jQuery的Ajax请求全局配置
摘要:jQuery已经成为项目中最常见的js库,也是前端开发最喜欢使用的库。下面是在项目中封装了jQuery的Ajax,分享给大家。 代码: 代码如下: -1)
requestType = 'json';
requestType = opt.dataType || requestType;
// 是否异步请求
var async = (opt.async === undefined ? true : opt.async);
return {
url: url,
async: async,
type: opt.type || 'get',
dataType: requestType,
cache: false,
data: opt.data,
success: function(data,textStatus,xhr) {
/*
*如果dataType是json,怎判断返回数据是否为json格式,如果不是进行转换
* 成功数据通用格式
* {
* "code": 200,
* "data": [],
* "success": true // 成功
* }
* 失败返回的数据
* {
* "code": 200,
* "info": 'error',
* "success": false // 失败
* }
*/
if((requestType === 'json' || requestType === "jsonp") && typeof(data) === "string") {
data = JSON.parse(data);
}
if(data.success) {
opt.success(data);
}
if(opt.error) { opt.error(data); } }, error: function(xhr,status,handler) { if (opt.error) opt.error(); } }; }; function unescapeEntity(str) { var reg = /&(?:nbsp|#160|lt|#60|gt|62|amp|#38|quot|#34|cent|#162|pound|#163|yen|#165|euro|#8364|sect|#167|copy|#169|reg|#174|trade|#8482|times|#215|divide|#247);/g, entity = { '' : ' ', ' ' : ' ', '<' : '<', '<' : '<', '>' : '>', '&62;' : '>', '&' : '&', '&' : '&', '"' : '"', '"' : '"', '¢' : '¢', '¢' : '¢', '£' : '£', '£' : '£', '¥' : '¥', '¥' : '¥', '€' : ' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |