Encapsulate ajax,
1 <pre class = "brush: js"> 2 <script type = "text/javascript"> 3; (function ($, window, document, undefined) {4 // jquery prototype Extension 5 $. extend ({6/* 7 url: request address 8 type: Request type 9 dataType: data type 10 data: request data 11 btnSet: Request initiation button, {id: button id, beforeText: Click the text displayed before, afterText: Text displayed in the request} 12 successFun: Success 13 */14 getAjax: function (url, type, dataType, data, successFun, errorFun, btnset) {15 var _ this = this; 16 return $. ajax ({17 url: url, 18 type: type, 19 dataType: dataType | 'json', 20 data: data, 21 beforeSend: function () {22 if (btnset & btnset. $ btn) {23 var btn = btnset. $ btn; 24 btn.html (btnset. afterText | ''). prop ('Disabled ', true); 25} 26}, 27 complete: function () {28 setTimeout (function () {29 if (btnset & btnset. $ btn) {30 var btn = btnset. $ btn; 31 btn.html (btnset. beforeText | ''). prop ('Disabled ', false) 32} 33}, 1000) 34}, 35 success: function (data) {36 successFun & successFun (data) 37 38 }, 39 error: function (xhr, st, msg) {40 var errormsg; 41 if (_ this. serverConfig. status [st]) {42 errormsg = _ this. serverConfig. status [st]; 43} else {44 errormsg = 'request failed'; 45} 46 if (errormsg & errorfunc) errorfunc (errormsg); 47} 48 }) 49} 50}) 51} (jQuery, window, document); 52 </script> 53 </pre>