使用Jquery调用Asp.net生成的webservice Call Asp.net use Jquery generated webservice

2007年08月08日• 追风逐月 • 分类: 技术开发 , 网站建设 August 8, 2007 • • Zhuifeng monthly Category: technology development, construction site

厌倦了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

PLAIN TEXT PLAIN TEXT

JavaScript: JavaScript:

  1. // 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>

  2. $ ( document ) . ready ( function ( ) { $ (Document). Ready (function () (

  3. $. ajaxSetup ( { $. AjaxSetup ((

  4. url: “/Service/Service.asmx/” , Url: "/ Service / Service.asmx /"

  5. global: false , Global: false,

  6. dataType: “json” , DataType: "json"

  7. contentType: ‘application/json; charset=utf-8′ , ContentType: 'application / json; charset = utf-8',

  8. type: “POST” Type: "POST"

  9. } ) ; ));

  10. } )

其次,因为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

PLAIN TEXT PLAIN TEXT

JavaScript: JavaScript:

  1. //json.js / / Json.js

  2. //modified by chyni / / Modified by chyni

  3. ( function ( $ ) { (Function ($) (

  4. var m = { Var m = (

  5. \b : \\ b’ , '\ B': '\ \ b',

  6. \t : \\ t’ , '\ T': '\ \ t',

  7. \n : \\ n’ , '\ N': '\ \ n',

  8. \f : \\ f’ , '\ F': '\ \ f',

  9. \r : \\ r’ , '\ R': '\ \ r',

  10. ‘”‘ : \\ “‘ , ' "': '\ \"'

  11. \\ : \\ \\ '\ \': '\ \ \ \'

  12. } , )

  13. s = { S = (

  14. ‘array’ : function ( x ) { 'Array': function (x) (

  15. var a = [ ‘[’ ] , b, f, i, l = x. length , v; Var a = [ '['], b, f, i, l = x. length, v;

  16. for ( i = 0 ; i <l; i = 1 ) { For (i = 0; i <l; i = 1) (

  17. v = x [ i ] ; V = x [i];

  18. f = s [ typeof v ] ; F = s [typeof v];

  19. if ( f ) { If (f) (

  20. v = f ( v ) ; V = f (v);

  21. if ( typeof v == ’string’ ) { If (typeof v == 'string') (

  22. if ( b ) { If (b) (

  23. a [ a. length ] = ‘,’ ; A [a. length] = '';

  24. } )

  25. a [ a. length ] = v; A [a. length] = v;

  26. b = true ; B = true;

  27. } )

  28. } )

  29. } )

  30. a [ a. length ] = ‘]’ ; A [a. length] = ']';

  31. return a. join ( ) ; Return a. join ( ");

  32. } , )

  33. ‘boolean’ : function ( x ) { 'Boolean': function (x) (

  34. return String ( x ) ; Return String (x);

  35. } , )

  36. ‘null’ : function ( x ) { 'Null': function (x) (

  37. return “null” ; Return "null";

  38. } , )

  39. ‘number’ : function ( x ) { 'Number': function (x) (

  40. return isFinite ( x ) ? String ( x ) : ‘null’ ; Return isFinite (x)? String (x): 'null';

  41. } , )

  42. ‘object’ : function ( x ) { 'Object': function (x) (

  43. if ( x ) { If (x) (

  44. if ( x instanceof Array ) { If (x instanceof Array) (

  45. return s. array ( x ) ; Return s. array (x);

  46. } )

  47. var a = [ ‘{’ ] , b, f, i, v; Var a = [ '('], b, f, i, v;

  48. for ( i in x ) { For (i in x) (

  49. v = x [ i ] ; V = x [i];

  50. f = s [ typeof v ] ; F = s [typeof v];

  51. if ( f ) { If (f) (

  52. v = f ( v ) ; V = f (v);

  53. if ( typeof v == ’string’ ) { If (typeof v == 'string') (

  54. if ( b ) { If (b) (

  55. a [ a. length ] = ‘,’ ; A [a. length] = '';

  56. } )

  57. a. push ( s. string ( i ) , ‘:’ , v ) ; A. push (s. string (i), ':', v);

  58. b = true ; B = true;

  59. } )

  60. } )

  61. } )

  62. a [ a. length ] = ‘}’ ; A [a. length] = ')';

  63. return a. join ( ) ; Return a. join ( ");

  64. } )

  65. return ‘null’ ; Return 'null';

  66. } , )

  67. ’string’ : function ( x ) { 'String': function (x) (

  68. if ( / [ \\ \x 00- \x 1f]/.test(x)) { If (/ [ "\ \ \ x 00 - \ x 1f] /. Test (x)) (

  69. x = x.replace(/([ \x 00- \x 1f \\ ] ) /g, function ( a, b ) { X = x.replace (/ ([\ x 00 - \ x 1f \ \ "]) / g, function (a, b) (

  70. var c = m [ b ] ; Var c = m [b];

  71. if ( c ) { If (c) (

  72. return c; Return c;

  73. } )

  74. c = b. charCodeAt ( ) ; C = b. charCodeAt ();

  75. return \\ u00′ Return '\ \ u00'

  76. Math. floor ( c / 16 ) . toString ( 16 ) Math. Floor (c / 16). ToString (16)

  77. ( c % 16 ) . toString ( 16 ) ; (C% 16). ToString (16);

  78. } ) ; ));

  79. } )

  80. return ‘”‘ x ‘”‘ ; Return ' "' x '"';

  81. } )

  82. } ; );

  83. $. toJSON = function ( v ) { $. ToJSON = function (v) (

  84. var f = isNaN ( v ) ? s [ typeof v ] : s [ ‘number’ ] ; Var f = isNaN (v)? S [typeof v]: s [ 'number'];

  85. if ( f ) return f ( v ) ; If (f) return f (v);

  86. } ; );

  87. $. parseJSON = function ( v, safe ) { $. ParseJSON = function (v, safe) (

  88. if ( safe === undefined ) safe = $. parseJSON . safe ; If (safe === undefined) = $ safe. ParseJSON. Safe;

  89. 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))

  90. return undefined; Return undefined;

  91. return eval(’(' v ’)'); Return eval ('(' v ')');

  92. }; );

  93. $.parseJSON.safe = false; $. ParseJSON.safe = false;

  94. })(jQuery); )) (JQuery);

  95. $.postJOSN=function(url, data, callback ) $. PostJOSN = function (url, data, callback)

  96. { return $.post(url, $.toJSON(data),function(ret){callback($.parseJSON(ret,true)); }); (Return $. Post (url, $. ToJSON (data), function (ret) (callback ($. ParseJSON (ret, true));));

  97. } )

这样,我们就可以如下方便的调用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.

标签: asp.net , google , javascript Tags: asp.net, google, javascript
本文链接: http://chyni.com/site-builder/jquery-asmx.html This paper links: http://chyni.com/site-builder/jquery-asmx.html

你可能还喜欢 You may also like



发表你的意见 Express your views

追风逐月 Zhuifeng monthly

提供网络赚钱、网赚经验、网赚工具、SEO、国外主机信息 The provision of the network to make money, Wangzhuan experience Wangzhuan tool, SEO, foreign host information
Herd monthly subscription blog feed
Subscribe to google Subscribe to Battery
Subscribe to the fresh fruit Subscribe to grasp shrimp
* 更多订阅本站方式请看订阅帮助 * See More Subscribe way to help subscribers