首页 > 代码库 > 为什么要有$.get $.post呢,这两种方法用在什么地方呢
为什么要有$.get $.post呢,这两种方法用在什么地方呢
我一般用自己的一套ajax,也蛮好用的:
Java code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/** 全局ajax对象 */ var ajax = new Object(); var ajaxCore = null ; /** * @see 创建ajax核心对象,兼容浏览器有:IE6,7,8,9,谷歌,火狐,欧朋,360极速,360安全,苹果,搜狗,遨游,猎豹,腾讯 * @return XMLHttpRequest */ ajax.getCore = function() { var xmlHttp = null ; if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); if (xmlHttp.overrideMimeType) { xmlHttp.overrideMimeType( "text/xml" ); } } else { if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" ); } catch (e) { xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } } } if (!xmlHttp) { window.alert( "\u8bf7\u4f7f\u7528IE\u6d4f\u89c8\u5668!" ); } return xmlHttp; }; /** * @see 处理ajax参数 * @param param 参数 * @return String */ ajax.getParam = function(param) { var randomStr = "ajaxParamRandom=" + Math.random(); if (param == null || param == "" ) { return randomStr; } var str = "" ; if (typeof(param) == "object" ) { for (var key in param) { str += key + "=" + param[key] + "&" ; } str = (str.length > 0 ? str.substring( 0 , str.length - 1 ) : str); } else { str = param; } return str + "&" + randomStr; }; /** * @see ajax的回调函数 * @param callback 用户自定义回调函数 * @param url 请求的url */ ajax.doCallback = function(callback, url) { if (ajaxCore.readyState == 4 ) { if (ajaxCore.status == 200 ) { if (callback == null ) { return ; } var result = new String(ajaxCore.responseText); if ( null != result && result != "" ) { if (result == "null" ) { callback( null ); } else { var backObject = null ; if (result == "true" || result == "false" ) { backObject = eval(result); } else if (!isNaN(result)) { backObject = parseFloat(result); } else if ((result.substring( 0 , 1 ) == "[" && result.substring(result.length - 1 , result.length) == "]" ) || (result.substring( 0 , 1 ) == "{" && result.substring(result.length - 1 , result.length) == "}" )) { backObject = eval( "(" + result + ")" ); } else { backObject = result; } callback(backObject); } } else { callback(result); } } else if (ajaxCore.status == 0 || ajaxCore.status == 12029 ) { showProgress( "0" , "1" ); alertWin( "\u627e\u4e0d\u5230\u670d\u52a1\u5668\uff01" , null , "" ); } else if (ajaxCore.status == 404 ) { showProgress( "0" , "1" ); alertWin( "\u627e\u4e0d\u5230\u8d44\u6e90: " + url, null , "" ); } } }; /** * @see ajax的post请求 * @param url 请求的url * @param param 参数列表,可以是字符串或js对象或null * @param callback 用户自定义回调函数 */ ajax.post = function(url, param, callback) { ajaxCore = ajax.getCore(); if (ajaxCore != null ) { ajaxCore.onreadystatechange = function() { ajax.doCallback(callback, url); }; ajaxCore.open( "POST" , url, true ); ajaxCore.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ); ajaxCore.send(ajax.getParam(param)); } }; /** * @see ajax的get请求 * @param url 请求的url * @param callback 用户自定义回调函数 */ ajax.get = function(url, callback) { ajaxCore = ajax.getCore(); if (ajaxCore != null ) { if (url.indexOf( "?" ) != - 1 ) { url += "&ajaxParamRandom=" + Math.random(); } else { url += "?ajaxParamRandom=" + Math.random(); }alert(url); ajaxCore.onreadystatechange = function() { ajax.doCallback(callback, url); }; ajaxCore.open( "GET" , url, true ); ajaxCore.send( null ); } }; |
以上代码写在一个单独的js文件中,其它地方引入,调用方式如下:
Java code
?
1
2
3
4
5
|
showProgress( "1" , "1" ); // ajax进度条: 正在提交,请稍候.... ajax.post( "/oc/iva.htm" , {id: 1 , version: ‘1.2.6‘ }, function(json) { showProgress( "0" , "1" ); // 关闭进度条 alert(json.success); }); |
我一般用自己的一套ajax,也蛮好用的:
Java code?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
/** 全局ajax对象 */ var ajax = new Object(); var ajaxCore = null ; /** * @see 创建ajax核心对象,兼容浏览器有:IE6,7,8,9,谷歌,火狐,欧朋,360极速,360安全,苹果,搜狗,遨游,猎豹,腾讯 * @return XMLHttpRequest */ ajax.getCore = function() { var xmlHttp = null ; if (window.XMLHttpRequest) { xmlHttp = new XMLHttpRequest(); if (xmlHttp.overrideMimeType) { xmlHttp.overrideMimeType( "text/xml" ); } } else { if (window.ActiveXObject) { try { xmlHttp = new ActiveXObject( "Msxml2.XMLHTTP" ); } catch (e) { xmlHttp = new ActiveXObject( "Microsoft.XMLHTTP" ); } } } if (!xmlHttp) { window.alert( "\u8bf7\u4f7f\u7528IE\u6d4f\u89c8\u5668!" ); } return xmlHttp; }; /** * @see 处理ajax参数 * @param param 参数 * @return String */ ajax.getParam = function(param) { var randomStr = "ajaxParamRandom=" + Math.random(); if (param == null || param == "" ) { return randomStr; } var str = "" ; if (typeof(param) == "object" ) { for (var key in param) { str += key + "=" + param[key] + "&" ; } str = (str.length > 0 ? str.substring( 0 , str.length - 1 ) : str); } else { str = param; } return str + "&" + randomStr; }; /** * @see ajax的回调函数 * @param callback 用户自定义回调函数 * @param url 请求的url */ ajax.doCallback = function(callback, url) { if (ajaxCore.readyState == 4 ) { if (ajaxCore.status == 200 ) { if (callback == null ) { return ; } var result = new String(ajaxCore.responseText); if ( null != result && result != "" ) { if (result == "null" ) { callback( null ); } else { var backObject = null ; if (result == "true" || result == "false" ) { backObject = eval(result); } else if (!isNaN(result)) { backObject = parseFloat(result); } else if ((result.substring( 0 , 1 ) == "[" && result.substring(result.length - 1 , result.length) == "]" ) || (result.substring( 0 , 1 ) == "{" && result.substring(result.length - 1 , result.length) == "}" )) { backObject = eval( "(" + result + ")" ); } else { backObject = result; } callback(backObject); } } else { callback(result); } } else if (ajaxCore.status == 0 || ajaxCore.status == 12029 ) { showProgress( "0" , "1" ); alertWin( "\u627e\u4e0d\u5230\u670d\u52a1\u5668\uff01" , null , "" ); } else if (ajaxCore.status == 404 ) { showProgress( "0" , "1" ); alertWin( "\u627e\u4e0d\u5230\u8d44\u6e90: " + url, null , "" ); } } }; /** * @see ajax的post请求 * @param url 请求的url * @param param 参数列表,可以是字符串或js对象或null * @param callback 用户自定义回调函数 */ ajax.post = function(url, param, callback) { ajaxCore = ajax.getCore(); if (ajaxCore != null ) { ajaxCore.onreadystatechange = function() { ajax.doCallback(callback, url); }; ajaxCore.open( "POST" , url, true ); ajaxCore.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" ); ajaxCore.send(ajax.getParam(param)); } }; /** * @see ajax的get请求 * @param url 请求的url * @param callback 用户自定义回调函数 */ ajax.get = function(url, callback) { ajaxCore = ajax.getCore(); if (ajaxCore != null ) { if (url.indexOf( "?" ) != - 1 ) { url += "&ajaxParamRandom=" + Math.random(); } else { url += "?ajaxParamRandom=" + Math.random(); }alert(url); ajaxCore.onreadystatechange = function() { ajax.doCallback(callback, url); }; ajaxCore.open( "GET" , url, true ); ajaxCore.send( null ); } }; |
以上代码写在一个单独的js文件中,其它地方引入,调用方式如下:
Java code?
1
2
3
4
5
|
showProgress( "1" , "1" ); // ajax进度条: 正在提交,请稍候.... ajax.post( "/oc/iva.htm" , {id: 1 , version: ‘1.2.6‘ }, function(json) { showProgress( "0" , "1" ); // 关闭进度条 alert(json.success); }); |
为什么要有$.get $.post呢,这两种方法用在什么地方呢
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。