[C #] WebService using JSON format to pass the pen + JQuery test
0 2
Because of some factors, it is necessary to change the webservice, very traditional, but a lot of companies are still in use.
Because of the relationship between XML and not wanting him to pass the material, I had to try to clean up the JSON.
So I started my journey to rewrite.
0 2
First of all, there are a lot of files on the Internet, maybe because of the different situation, I have been confused many times by many tests.
Finally, we find the answer. Take a moment to remember.
0 2
First I started with three different WebMethod to test three different outputs.
0 2
getuserinfostring– Get string
Getoneuserinfo-Get an object
Getusers-Get the objects
0 2
Using System.Collections.Generic;Using System.Web. Script.services;Using System.Web. Services;Namespace jsonservicesample{PublicClass User {PublicString Name {get; set;}Publicint age {get; set;}} [WebService (Namespace ="", Description ="For Donma Test")] [System.ComponentModel.ToolboxItem (False)] [ScriptService]PublicClassService1:webservice {[WebMethod] [Scriptmethod (Responseformat = Responseformat.Json)]PublicString Getuserinfostring (String name,int age) {Return name +"," + age; } [WebMethod] [Scriptmethod (Responseformat = Responseformat.Json)]Public User Getoneuserinfo (string name, int.) { return (new User {name = name, age = Age}); [WebMethod] [Scriptmethod (Responseformat = Responseformat. Json)] public user[] Getusers (string name, Int. age) {list<user> res = new list<user> (); Res. ADD (new User {name = name + "1", age = Age}); Res. ADD (new User {name = name + "2", age = Age}); return Res. ToArray (); } }}
0 2
If this 0 2 [C #] Web Service removes xmlns 0 2 I'll first remove the XML namespace
One more point, and I'll add it on top of each method.
0 2
0 2
[WebMethod] [Scriptmethod (Responseformat = Responseformat. Json)]
Because sometimes I need to use get to pass the pass so I'm adding it in Web Config
In System. Join in the Web
0 2
<WebServices><protocols><< Span style= "color: #800000;" >add name= "HttpGet" / ><add name= "HttpPost" />< add name= " Documentation "/></ protocols></< Span style= "color: #800000;" >webservices>
0 2
Web. Config Full text:
0 2
<?XmlVersion= "1.0"?><!--For more information on how to configure your ASP. Application, please visithttp://go.microsoft.com/fwlink/?LinkId=169433-<Configuration><configsections><SectiongroupName= "ApplicationSettings"Type= "System.Configuration.ApplicationSettingsGroup, System, version=4.0.0.0, Culture=neutral, publickeytoken= b77a5c561934e089 "><SectionName= "JsonServiceSample.Properties.Settings"Type= "System.Configuration.ClientSettingsSection, System, version=4.0.0.0, Culture=neutral, publickeytoken= b77a5c561934e089 "Requirepermission= "false"/></Sectiongroup></configsections><System.Web><CompilationDebug= "true"TargetFramework= "4.0"/><httphandlers></httphandlers><WebServices><Protocols><AddName= "HttpGet"/><AddName= "HttpPost"/><AddName= "Documentation"/></Protocols></WebServices></System.Web><ApplicationSettings><JsonServiceSample.Properties.Settings><SettingName= "Jsonservicesample_jtestservice_Service1 "SerializeAs= "String"> <value>http://localhost:5403/Service1.asmx</value > </setting> </JsonServiceSample.Properties.Settings> </ applicationsettings></configuration>
0 2
So try running.
0 2
0 2
Strange why not JSON , don't be nervous ... We continue to use JQuery to call and see.
0 2
JQuery Code:
0 2
0 2
<typeIDvalue<type= "Text/javascript">
2: function GetInfo () {
3: var $res;
4: $.ajax ({
5: "POST",
6: "Service1.asmx/getoneuserinfo",
7: "application/json; Charset=utf-8 ",
8: false,
9: false,
Ten: 'json',
One: "{name: ' When hemp ', age:29}",
: Function (data) {
: if (Data.hasownproperty ("D")) {
: $res = DATA.D;
: }
: Else
: $res = data;
: }
: });
: return $res;
: }
22:
: $ (' #ipt1 '). Click (function () {
: var res = GetInfo ();
: alert (res. Name);
: });
28:
29:
</Script>
Press the button down and I'll have him call Getoneuserinfo this method.
and use Post
Look at the results.
0 2
0 2
nn. is JSON, but one more D 0 2 with __type basically 0 2 __type do not move he does not affect, but 0 2 D this thing must be handled.
So I'll take this article: http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/
Change to write. The three different values of the above are measured. All ok~~
0 2
This will not affect the traditional WebService Reference call 0 2 WebService.
You can also pass through the JSONthrough the jquery call ...
Take a moment to remember. To the people in need ...
WebService developed by C # uses JSON format to pass data +ajax test