使用Jquery调用Asp.net生成的webservice Call Asp.net use Jquery generated webservice
厌倦了very very不爽的ms ajax.net类库,终于决定要使用jquery把它替换掉。不爽very very tired of the ms ajax.net class library, and finally decided to use jquery replace it.
Jquery的好处,就不用说了,简单,方便,人见人爱,车见车载啊… … Jquery benefits, needless to say, a simple, convenient and cute love to see car ah……
ms ajax调用的方式一般为 Ms ajax call for the General
[c#] [C #]
Service.方法名(参数,结果处理函数,失败处理函数,其他参数); Service. Method (parameters, results processing function, failure handling functions and other parameters);
[/c#] [/ C #]
使用firebug监控,发现提交方式为: post Use firebug monitoring, found that the way: post
调用地址为:/Service/Service.asmx/方法名如/Service/Service.asmx/GetSample Calling Address: / Service / Service.asmx / method names such as / Service / Service.asmx / GetSample
其参数格式如下 Its parameters are as follows format
‘{name:value,name1:value1,…}’ '(Name: value, name1: value1,…)'
也就是说是javascript object 的json字符串格式 In other words javascript object is the format string json
Content-Type=application/json; charset=utf-8 Content-Type = application / json; charset = utf-8
这样,我们就可以用jquery来模拟ms ajax的调用过程了 In this way, we can be used to simulate jquery ms ajax call the process
首先设置Ajax环境 Ajax initially environment
JavaScript: JavaScript:
// write by <a href=”http://www.chyni.cn” class=”alinks_links” onclick=”return alinks_click(this);” title=”" style=”padding-right: 13px; background: url(http://www.chyni.cn/wp-content/plugins/alinks/images/external.png) center right no-repeat;” rel=”external”>chyni</a> / / Write by <a href = "http://www.chyni.cn" class = "alinks_links" onclick = "return alinks_click (this);" title = "" style = "padding-right: 13px; background: url (http://www.chyni.cn/wp-content/plugins/alinks/images/external.png) center right no-repeat "rel =" external "> chyni </ a>
$ ( document ) . ready ( function ( ) { $ (Document). Ready (function () (
$. ajaxSetup ( { $. AjaxSetup ((
url: “/Service/Service.asmx/” , Url: "/ Service / Service.asmx /"
global: false , Global: false,
dataType: “json” , DataType: "json"
contentType: ‘application/json; charset=utf-8′ , ContentType: 'application / json; charset = utf-8',
type: “POST” Type: "POST"
} ) ; ));
} )
其次,因为jquery没有直接提供postJSON方法,于是自己实现了一个$.postJSON()方法其中使用了json.js插件,该插件地址国内无法访问,摘录代码如下 Secondly, because there is no direct provision postJSON jquery, therefore realized a $ own. PostJSON () method which uses json.js plug-in, plug-in the domestic addresses can not be accessed, extract the code below
JavaScript: JavaScript:
//json.js / / Json.js
//modified by chyni / / Modified by chyni
( function ( $ ) { (Function ($) (
var m = { Var m = (
‘ \b ‘ : ‘ \\ b’ , '\ B': '\ \ b',
‘ \t ‘ : ‘ \\ t’ , '\ T': '\ \ t',
‘ \n ‘ : ‘ \\ n’ , '\ N': '\ \ n',
‘ \f ‘ : ‘ \\ f’ , '\ F': '\ \ f',
‘ \r ‘ : ‘ \\ r’ , '\ R': '\ \ r',
‘”‘ : ‘ \\ “‘ , ' "': '\ \"'
‘ \\ ‘ : ‘ \\ \\ ‘ '\ \': '\ \ \ \'
} , )
s = { S = (
‘array’ : function ( x ) { 'Array': function (x) (
var a = [ ‘[’ ] , b, f, i, l = x. length , v; Var a = [ '['], b, f, i, l = x. length, v;
for ( i = 0 ; i <l; i = 1 ) { For (i = 0; i <l; i = 1) (
v = x [ i ] ; V = x [i];
f = s [ typeof v ] ; F = s [typeof v];
if ( f ) { If (f) (
v = f ( v ) ; V = f (v);
if ( typeof v == ’string’ ) { If (typeof v == 'string') (
if ( b ) { If (b) (
a [ a. length ] = ‘,’ ; A [a. length] = '';
} )
a [ a. length ] = v; A [a. length] = v;
b = true ; B = true;
} )
} )
} )
a [ a. length ] = ‘]’ ; A [a. length] = ']';
return a. join ( ” ) ; Return a. join ( ");
} , )
‘boolean’ : function ( x ) { 'Boolean': function (x) (
return String ( x ) ; Return String (x);
} , )
‘null’ : function ( x ) { 'Null': function (x) (
return “null” ; Return "null";
} , )
‘number’ : function ( x ) { 'Number': function (x) (
return isFinite ( x ) ? String ( x ) : ‘null’ ; Return isFinite (x)? String (x): 'null';
} , )
‘object’ : function ( x ) { 'Object': function (x) (
if ( x ) { If (x) (
if ( x instanceof Array ) { If (x instanceof Array) (
return s. array ( x ) ; Return s. array (x);
} )
var a = [ ‘{’ ] , b, f, i, v; Var a = [ '('], b, f, i, v;
for ( i in x ) { For (i in x) (
v = x [ i ] ; V = x [i];
f = s [ typeof v ] ; F = s [typeof v];
if ( f ) { If (f) (
v = f ( v ) ; V = f (v);
if ( typeof v == ’string’ ) { If (typeof v == 'string') (
if ( b ) { If (b) (
a [ a. length ] = ‘,’ ; A [a. length] = '';
} )
a. push ( s. string ( i ) , ‘:’ , v ) ; A. push (s. string (i), ':', v);
b = true ; B = true;
} )
} )
} )
a [ a. length ] = ‘}’ ; A [a. length] = ')';
return a. join ( ” ) ; Return a. join ( ");
} )
return ‘null’ ; Return 'null';
} , )
’string’ : function ( x ) { 'String': function (x) (
if ( / [ “ \\ \x 00- \x 1f]/.test(x)) { If (/ [ "\ \ \ x 00 - \ x 1f] /. Test (x)) (
x = x.replace(/([ \x 00- \x 1f \\ “ ] ) /g, function ( a, b ) { X = x.replace (/ ([\ x 00 - \ x 1f \ \ "]) / g, function (a, b) (
var c = m [ b ] ; Var c = m [b];
if ( c ) { If (c) (
return c; Return c;
} )
c = b. charCodeAt ( ) ; C = b. charCodeAt ();
return ‘ \\ u00′ Return '\ \ u00'
Math. floor ( c / 16 ) . toString ( 16 ) Math. Floor (c / 16). ToString (16)
( c % 16 ) . toString ( 16 ) ; (C% 16). ToString (16);
} ) ; ));
} )
return ‘”‘ x ‘”‘ ; Return ' "' x '"';
} )
} ; );
$. toJSON = function ( v ) { $. ToJSON = function (v) (
var f = isNaN ( v ) ? s [ typeof v ] : s [ ‘number’ ] ; Var f = isNaN (v)? S [typeof v]: s [ 'number'];
if ( f ) return f ( v ) ; If (f) return f (v);
} ; );
$. parseJSON = function ( v, safe ) { $. ParseJSON = function (v, safe) (
if ( safe === undefined ) safe = $. parseJSON . safe ; If (safe === undefined) = $ safe. ParseJSON. Safe;
if ( safe && !/^ ( “( \\ .|[^” \\\n\r ] ) *? “|[,:{} \[ \] 0-9. \- Eaeflnr-u \n \r \t ]) ?$/.test(v)) If (& safe! / ^ ( "(\ \ .|[^" \ \ \ N \ r]) *? "|[,:{} \ [\] 0-9. \ - Eaeflnr-u \ n \ r \ t ]) ?$/. test (v))
return undefined; Return undefined;
return eval(’(' v ’)'); Return eval ('(' v ')');
}; );
$.parseJSON.safe = false; $. ParseJSON.safe = false;
})(jQuery); )) (JQuery);
$.postJOSN=function(url, data, callback ) $. PostJOSN = function (url, data, callback)
{ return $.post(url, $.toJSON(data),function(ret){callback($.parseJSON(ret,true)); }); (Return $. Post (url, $. ToJSON (data), function (ret) (callback ($. ParseJSON (ret, true));));
} )
这样,我们就可以如下方便的调用asp.net的webservice了 In this way, we can call the following convenient asp.net of the webservice
$.postJOSN(’/Service/Service.asmx/GetSample’, $. PostJOSN ( '/ Service / Service.asmx / GetSample'
{”msg”:’我爱追风 ‘}, ( "Msg": 'I love Zhuifeng'),
function(data){GetSampleResult(data,itype);} Function (data) (GetSampleResult (data, itype);)
);
再也不用忍受臃肿的Microsoft AJAX Framework了,这下天下太平了。 Can not tolerate bloated Microsoft AJAX Framework, under which the Tianxiatabeng.




