程序ajax请求公共组件:app-jquery-http.js
1 // --------网络操作--------------------
2 $.HTTP = {
3 getUrlParam : function(name) {
4 var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
5 var r = window.location.search.substr(1).match(reg);
6 if (r != null)
7 return unescape(r[2]);
8 return null;
9 },
10 // 设置url参数值,ref参数名,value新的参数值
11 setUrlParam:function (url,ref, value)
12 {
13 var str = "";
14 if (url.indexOf('?') != -1)
15 str = url.substr(url.indexOf('?') + 1);
16 else
17 return url + "?" + ref + "=" + value;
18 var returnurl = "";
19 var setparam = "";
20 var arr;
21 var modify = "0";
22 if (str.indexOf('&') != -1) {
23 arr = str.split('&');
24 for (i in arr) {
25 if (arr[i].split('=')[0] == ref) {
26 setparam = value;
27 modify = "1";
28 }
29 else {
30 setparam = arr[i].split('=')[1];
31 }
32 returnurl = returnurl + arr[i].split('=')[0] + "=" + setparam + "&";
33 }
34 returnurl = returnurl.substr(0, returnurl.length - 1);
35 if (modify == "0")
36 if (returnurl == str)
37 returnurl = returnurl + "&" + ref + "=" + value;
38 }
39 else {
40 if (str.indexOf('=') != -1) {
41 arr = str.split('=');
42 if (arr[0] == ref) {
43 setparam = value;
44 modify = "1";
45 }
46 else {
47 setparam = arr[1];
48 }
49 returnurl = arr[0] + "=" + setparam;
50 if (modify == "0")
51 if (returnurl == str)
52 returnurl = returnurl + "&" + ref + "=" + value;
53 }
54 else
55 returnurl = ref + "=" + value;
56 }
57 return url.substr(0, url.indexOf('?')) + "?" + returnurl;
58 },
59
60 // 删除参数值
61 deleteUrlParam:function (url,ref) {
62 var str = "";
63 if (url.indexOf('?') != -1) {
64 str = url.substr(url.indexOf('?') + 1);
65 }
66 else {
67 return url;
68 }
69 var arr = "";
70 var returnurl = "";
71 var setparam = "";
72 if (str.indexOf('&') != -1) {
73 arr = str.split('&');
74 for (i in arr) {
75 if (arr[i].split('=')[0] != ref) {
76 returnurl = returnurl + arr[i].split('=')[0] + "=" + arr[i].split('=')[1] + "&";
77 }
78 }
79 return url.substr(0, url.indexOf('?')) + "?" + returnurl.substr(0, returnurl.length - 1);
80 }
81 else {
82 arr = str.split('=');
83 if (arr[0] == ref) {
84 return url.substr(0, url.indexOf('?'));
85 }
86 else {
87 return url;
88 }
89 }
90 },
91 /**
92 *
93 * @param opt
94 * opt.url,postdata, success, failure, error
95 */
96 obj : function(opt) {
97
98 var formData;
99
100 if (opt.postType) {
101 switch (opt.postType) {
102 case "multipart":
103 formData = new FormData($(opt.formId)[0]);
104 break;
105 case "form":
106 if(opt.formId){
107 formData=$(opt.formId).serialize();
108 }else{
109 formData=$.param(opt.ajaxData);
110 }
111 if(opt.extData){
112 formData += "&"+$.param(opt.extData);
113 }
114 break;
115 case "json":
116 formData = JSON.stringify(opt.ajaxData);
117 break;
118 default:
119 return;
120 }
121 } else {
122 formData = opt.ajaxData;
123 }
124
125 var sopt = {
126 type : opt.type == undefined ? "post" : opt.type,
127 async : false,
128 url : opt.url,
129 data : formData,
130 dataType : "json",
131 success : function(json) {
132 if (json.stat == 1) {
133 if (opt.success != undefined)
134 opt.success(json.data);
135 } else {
136 if (opt.failure != undefined)
137 opt.failure(json);
138 else if (json.code != undefined) {
139 $.HTTP.show_code_err(json);
140 } else {
141 console.log("success不等于true 【url: " + opt.url + "】");
142 if (json.errorMessages != null
143 && json.errorMessages.length > 0) {
144 console.error("错误", json.errorMessages[0]);
145 }
146 }
147
148 }
149
150 },
151 error : function(XMLHttpRequest, textStatus, errorThrown) {
152 var info = "XMLHttpRequest:" + JSON.stringify(XMLHttpRequest)
153 + " ;textStatus:" + textStatus + "; errorThrown:"
154 + JSON.stringify(errorThrown) + "; 【" + opt.url + "】";
155 console.log(info);
156 if (opt.error != undefined)
157 opt.error(XMLHttpRequest, textStatus, errorThrown);
158 else {
159 console.error("请求错误", "系统发生请求错误,请联系管理员解决。");
160 }
161 }
162 };
163 if (opt.postType) {
164 switch (opt.postType) {
165 case "multipart":
166 sopt.async = false;
167 sopt.cache = false;
168 sopt.contentType = false;
169 sopt.processData = false;
170 break;
171 case "form":
172 sopt.contentType = 'application/x-www-form-urlencoded';
173 break;
174 case "json":
175 sopt.contentType = 'application/json';
176 sopt.dataType = "json";
177 break;
178 default:
179 return;
180 }
181 }
182 $.ajax($.extend(sopt, opt.ajaxOption));
183 },
184 list : function(opt) {
185
186 var formData;
187
188 if (opt.postType) {
189 switch (opt.postType) {
190 case "multipart":
191 formData = new FormData($(opt.formId)[0]);
192 break;
193 case "form":
194 if(opt.formId){
195 formData=$(opt.formId).serialize();
196 }else{
197 formData=$.param(opt.ajaxData);
198 }
199 if(opt.extData){
200 formData += "&"+$.param(opt.extData);
201 }
202 break;
203 case "json":
204 formData =JSON.stringify(opt.ajaxData);
205 break;
206 default:
207 return;
208 }
209 } else {
210 formData = opt.ajaxData;
211 }
212
213 var sopt = {
214 type : opt.type == undefined ? "get" : opt.type,
215 async : false,
216 url : opt.url,
217 data : formData,
218 dataType : "json",
219 success : function(json) {
220 if (json.stat == 1) {
221 if (opt.success != undefined)
222 opt.success(json.list, json.pageInfo);
223 } else {
224 if (opt.failure != undefined)
225 opt.failure(json);
226 else if (json.code != undefined) {
227 $.HTTP.show_code_err(json);
228 } else {
229 console.log("success不等于true 【url: " + opt.url + "】");
230 if (json.errorMessages != null
231 && json.errorMessages.length > 0) {
232 console.error("错误", json.errorMessages[0]);
233 }
234 }
235 }
236
237 },
238 error : function(XMLHttpRequest, textStatus, errorThrown) {
239 var info = "XMLHttpRequest:" + JSON.stringify(XMLHttpRequest)
240 + " ;textStatus:" + textStatus + "; errorThrown:"
241 + JSON.stringify(errorThrown) + "; 【" + opt.url + "】";
242 console.log(info);
243 if (opt.error != undefined)
244 opt.error(XMLHttpRequest, textStatus, errorThrown);
245 else {
246 console.error("请求错误", "系统发生请求错误,请联系管理员解决。");
247 }
248 }
249 };
250
251 if (opt.postType) {
252 switch (opt.postType) {
253 case "multipart":
254 sopt.async = false;
255 sopt.cache = false;
256 sopt.contentType = false;
257 sopt.processData = false;
258 break;
259 case "form":
260 sopt.contentType = 'application/x-www-form-urlencoded';
261 break;
262 case "json":
263 sopt.contentType = 'application/json';
264 sopt.dataType = "json";
265 break;
266 default:
267 return;
268 }
269 }
270
271 $.ajax($.extend(sopt, opt.ajaxOption));
272 },
273 show_code_err : function(json) {
274 if (json.code == 1403) {
275 showLogin();
276 return;
277 } else if (json.errorMessages != undefined
278 && json.errorMessages != null && json.errorMessages.length > 0) {
279 alert(json.errorMessages[0]);
280 console.error("错误", json.errorMessages[0]);
281 }
282
283 }
284 }