WebService使用JSON格式传递笔记+JQuery测试

简介: 原文 WebService使用JSON格式传递笔记+JQuery测试 因为一些因素,必须改写WebService,很传统,但是很多公司还在用.. 因为XML 的关系,不想让他传递数据的时候过度肥大,所以我必须要尽量干净的JSON.

原文 WebService使用JSON格式传递笔记+JQuery测试

  • 因为一些因素,必须改写WebService,很传统,但是很多公司还在用.. 因为XML 的关系,不想让他传递数据的时候过度肥大,所以我必须要尽量干净的JSON.. 于是开始我的改写旅程..   首先,网络上好多好多好多文件,可能因为状况不同,测试过许多也让我搞混很多次.. 最后有找到答案..笔记一下..   首先我开了三个不同的WebMethod 来测试三种不同的输出..   GetUserInfoString –取得字符串 GetOneUserInfo - 取得一个对象 GetUsers - 取得对象们

     

    01. using System.Collections.Generic;
    02. using System.Web.Script.Services;
    03. using System.Web.Services;
    04.  
    05. namespace JsonServiceSample
    06. {
    07.  
    08. public class User
    09. {
    10. public string Name { get; set; }
    11. public int Age { get; set; }
    12. }
    13.  
    14.  
    15. [WebService(Namespace = "", Description = "For Donma Test")]
    16. [System.ComponentModel.ToolboxItem(false)]
    17. [ScriptService]
    18. public class Service1 : WebService
    19. {
    20.  
    21.  
    22. [WebMethod]
    23. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    24. public string GetUserInfoString(string name, int age)
    25. {
    26. return name + "," + age;
    27. }
    28.  
    29.  
    30. [WebMethod]
    31. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    32. public User GetOneUserInfo(string name, int age)
    33. {
    34. return (new User { Name = name, Age = age });
    35.  
    36. }
    37.  
    38.  
    39. [WebMethod]
    40. [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    41. public User[] GetUsers(string name, int age)
    42. {
    43. List<User> res = new List<User>();
    44. res.Add(new User { Name = name + "1", Age = age });
    45. res.Add(new User { Name = name + "2", Age = age });
    46.  
    47. return res.ToArray();
    48. }
    49.  
    50. }
    51. }

    再来一个重点,在每一个Method 上方我都会加上www.it165.net

    [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

    因为基于有时候我会需要使用GET 方式传递 所以我在Web Config 中加入 在system.web 中加入

    <webServices> <protocols> <add name="HttpGet"/> <add  name="HttpPost" /> <add name="Documentation" /> </protocols> </webServices>   Web.Config 全文:

     

    01. <?xml version="1.0"?>
    02.  
    03. <!--
    04. For more information on how to configure your <a href="http://www.it165.net/pro/webasp/" target="_blank" class="keylink">ASP</a>.NET application, please visit
    06. -->
    07.  
    08. <configuration>
    09. <configSections>
    10. <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    11. <section name="JsonServiceSample.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    12. </sectionGroup>
    13. </configSections>
    14. <system.web>
    15. <compilation debug="true" targetFramework="4.0" />
    16. <httpHandlers>
    17. </httpHandlers>
    18.  
    19. <webServices>
    20. <protocols>
    21. <add name="HttpGet"/>
    22. <add  name="HttpPost" />
    23. <add name="Documentation" />
    24. </protocols>
    25. </webServices>
    26. </system.web>
    27.  
    28. <applicationSettings>
    29. <JsonServiceSample.Properties.Settings>
    30. <setting name="JsonServiceSample_JTestService_Service1" serializeAs="String">
    31. <value>http://localhost:5403/Service1.asmx</value>
    32. </setting>
    33. </JsonServiceSample.Properties.Settings>
    34. </applicationSettings>
    35. </configuration>

    这样试跑一下

     

    \  

    奇怪为什么不是JSON ,别紧张…我们继续使用 JQuery 来呼叫看看.. JQuery Code :  

     

    01. <input type="button" id="ipt1" value="test" />
    02.  
    03. <script type="text/javascript">
    04.  
    05. function GetInfo() {
    06. var $res;
    07. $.ajax({
    08. type: "POST",
    09. url: "Service1.asmx/GetOneUserInfo",
    10. contentType: "application/json; charset=utf-8",
    11. async: false,
    12. cache: false,
    13. dataType: 'json',
    14. data: "{name:'当麻',age:29}",
    15. success: function (data) {
    16. if (data.hasOwnProperty("d")) {
    17. $res = data.d;
    18. }
    19. else
    20. $res = data;
    21. }
    22. });
    23. return $res;
    24. }
    25.  
    26.  
    27. $('#ipt1').click(function () {
    28. var res = GetInfo();
    29. alert(res.Name);
    30. });
    31.  
    32.  
    33. </script>

    按钮按下去之后 我让他呼叫 GetOneUserInfo 这 method 并且使用POST 看下结果..

     

    \  

    恩恩..的确是JSON, 但是多了一个 d  跟 __type 基本上  __type 不要去动他是不影响,但是  d 这东西必须得处理.. 进行改写..实测过上面三种不同的回传值..都 OK~~ 这样对于传统的 WebService Reference 呼叫  WebService 不会有影响.. 也可以透过JQuery 呼叫传递透过JSON…

目录
相关文章
|
3月前
|
XML 存储 JSON
Python学习 -- 常用数据交换格式(CSV、XML、JSON)
Python学习 -- 常用数据交换格式(CSV、XML、JSON)
31 0
|
2月前
|
XML 机器学习/深度学习 JSON
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
29 0
在火狐浏览器调ajax获取json数据时,控制台提示“XML 解析错误:格式不佳”。
|
12天前
|
存储 JSON NoSQL
MongoDB的文档存储格式BSON和JSON的区别
MongoDB的文档存储格式BSON和JSON的区别
|
23天前
|
XML JSON JavaScript
使用JSON和XML:数据交换格式在Java Web开发中的应用
【4月更文挑战第3天】本文比较了JSON和XML在Java Web开发中的应用。JSON是一种轻量级、易读的数据交换格式,适合快速解析和节省空间,常用于API和Web服务。XML则提供更强的灵活性和数据描述能力,适合复杂数据结构。Java有Jackson和Gson等库处理JSON,JAXB和DOM/SAX处理XML。选择格式需根据应用场景和需求。
|
2月前
|
JSON fastjson Java
FastJSON操作各种格式的JSON数据
FastJSON处理各种格式的JSON数据
|
2月前
|
JSON C# 数据格式
C# 处理gzip格式的json
C# 处理gzip格式的json
19 0
|
3月前
|
JSON 数据格式
将json格式的数据快速转换为excel,使用在线工具轻松搞定
将json格式的数据快速转换为excel,使用在线工具轻松搞定
138 0
|
3月前
|
JSON 关系型数据库 MySQL
这个问题是由于Flink的Table API在处理MySQL数据时,将MULTISET类型的字段转换为了JSON格式
【1月更文挑战第17天】【1月更文挑战第84篇】这个问题是由于Flink的Table API在处理MySQL数据时,将MULTISET类型的字段转换为了JSON格式
34 1
|
3月前
|
JSON Java API
Spring Boot 无侵入式 实现API接口统一JSON格式返回
Spring Boot 无侵入式 实现API接口统一JSON格式返回
|
4月前
|
存储 JSON 数据挖掘
CSV和JSON格式的数据在python上的处理
CSV和JSON数据类型都是都是常见的两种在python中的数据分析类型,这里我有两个入门项目详细讲解这两种数据的处理。