1./**
2. * 自定义全局通用函数
3. *
4. * @author kouyang
5. */
6.(function($) {
7. $.sendGet = function(url, data, callback, errCallback, type){
8. return $.send(url, data, callback, errCallback, type, 'GET');
9. }
10.
11. $.sendPost = function(url, data, callback, errCallback, type){
12. return $.send(url, data, callback, errCallback, type, 'POST');
13. }
14.
15. $.sendPut = function(url, data, callback, errCallback, type){
16. data._method='PUT';
17. return $.send(url, data, callback, errCallback, type, 'POST');
18. }
19.
20. $.sendDelete = function(url, data, callback, errCallback, type){
21. data._method='DELETE';
22. return $.send(url, data, callback, errCallback, type, 'POST');
23. }
24.
25. $.send = function(url, data, callback, errCallback, dtype, stype){
26. if ( jQuery.isFunction( data ) ) {
27. if( !jQuery.isFunction( callback ) ){
28. dtype = callback;
29. errCallback = data;//没有定义错误处理方法时,采用成功的处理方式
30. }else{
31. dtype = errCallback;
32. errCallback = callback;
33. }
34. callback = data;
35. data = {};
36. }else if( !jQuery.isFunction( errCallback ) ) {
37. dtype = errCallback;
38. errCallback = callback;//没有定义错误处理方法时,采用成功的处理方式
39. }
40.
41. if(stype==null){
42. stype = "GET";
43. }
44.
45. //访问成功后回调
46. var cmpCallback = function( res, status ) {
47. var data = jQuery.parseJSON(res.responseText);
48. var code = 200;
49. try{
50. code = data.bq_status;
51. }catch(e){}
52.
53. }
54.
55. return jQuery.ajax({
56. type: stype,
57. url : url,
58. data: data,
59. success : function( data, status, xhr ){
60. var type = "success";
61. try{
62. type = data.bq_type;
63. }catch(e){}
64. if(type=="error"){
65. errCallback.call(this, data, status, xhr);
66. }else{
67. callback.call(this, data, status, xhr);
68. }
69. },
70. error : errCallback,
71. complete: cmpCallback,
72. dataType: dtype
73. });
74. }
75.
76.})(jQuery);
或者按照这种形式,封装一个,本人用不惯,key:value这种形式的啊ajax。。
求帮忙。
tryList:function(){
var xhr = null;
for(var i=0;i<arguments.length;i++){
var lambda = arguments[i];
try{xhr = lambda();break;}catch(e){}
}
return xhr;
},
init:function(){
return this.tryList(
function(){return new ActiveXObject('MSXML2.XMLHttp.6.0');},
function(){return new ActiveXObject('MSXML2.XMLHttp.3.0');},
function(){return new XMLHttpRequest();},
function(){return new ActiveXObject('MSXML2.XMLHttp.5.0');},
function(){return new ActiveXObject('MSXML2.XMLHttp.4.0');},
function(){return new ActiveXObject('Msxml2.XMLHTTP');},
function(){return new ActiveXObject('MSXML.XMLHttp');},
function(){return new ActiveXObject('Microsoft.XMLHTTP');}
) || null;
},
post:function(sUrl,sArgs,bAsync,fCallBack,errmsg){
var xhr = this.init();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.responseText){
if(fCallBack.constructor==Function){fCallBack(xhr);}
}else{
//alert(errmsg);
}
}
};
xhr.open('POST',encodeURI(sUrl),bAsync);
xhr.setRequestHeader('Content-Length',sArgs.length);
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.send(sArgs);
},
get:function(sUrl,bAsync,fCallBack,errmsg){
var xhr = this.init();
xhr.onreadystatechange = function(){
if(xhr.readyState == 4){
if(xhr.responseText){
if(fCallBack.constructor==Function){fCallBack(xhr);}
}else{
alert(errmsg);
}
}
};
xhr.open('GET',encodeURI(sUrl),bAsync);
xhr.send('Null');
}
}
//使用
ajax.post('url','key=value&key=value',true,function(xhr){alert(xhr.responseText)},"无内容返回");