WebService使用JSON格式传递笔记+JQuery测试
原文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"
>
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…
WebService使用JSON格式传递笔记+JQuery测试的更多相关文章
- C#开发的WebService使用JSON格式传递数据+Ajax测试
[C#] WebService 使用 JSON 格式傳遞筆記 + JQuery 測試 0 2 因為一些因素,必須改寫WebService,很傳統,但是很多公司還在用.. 因為XML 的關係,不想讓他 ...
- SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析
SpringMVC中使用Ajax POST请求以json格式传递参数服务端通过request.getParameter("name")无法获取参数值问题分析 一:问题demo展示 ...
- WebService返回json格式数据供苹果或者安卓程序调用
1.新建一个WebService. 2. /// <summary> /// DemoToJson 的摘要说明 /// </summary> [WebService(Names ...
- Java Servlet生成JSON格式数据并用jQuery显示
1.Servlet通过json-lib生成JSON格式的数据 import java.io.IOException;import java.io.PrintWriter;import java.uti ...
- jquery json 格式教程
介绍 我们知道AJAX技术能够使得每一次请求更加迅捷,对于每一次请求返回的不是整个页面,也仅仅是所需要返回的数据.通常AJAX通过返回XML格式的数据,然后再通过客户端复杂的JavaScript脚本解 ...
- Ajax datatype:'JSON'的error问题Status1:200,JSON格式
转自:http://blog.sina.com.cn/s/blog_6e001be701017rux.html <script src="../js/jquery-1.8.0-vsdo ...
- ASP.net jQuery调用webservice返回json数据的一些问题
之前寒假时,试着使用jQuery写了几个异步请求demo, 但是那样是使用的webform普通页面,一般应该是用 webservice 居多. 最近写后台管理时,想用异步来实现一些信息的展示和修改, ...
- jquery ajax调用返回json格式数据处理
Ajax请求默认的都是异步的 如果想同步 async设置为false就可以(默认是true) var html = $.ajax({ url: "some.php", async: ...
- jQuery中使用Ajax获取JSON格式数据示例代码
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式.JSONM文件中包含了关于“名称”和“值”的信息.有时候我们需要读取JSON格式的数据文件,在jQuery中 ...
随机推荐
- windows 设置脚本IP
毫无疑问,在windows设置IP非常方便,因为有操作简单,直观的界面.通过图形用户界面设置IP在一般情况下是足够.但是,对于那些谁经常出差,由人产生的转换工作,这样的变化IP无疑耗时且不方便.假设一 ...
- Finding awesome developers in programming interviews(转)
英文原文:Finding awesome developers in programming interviews 我曾在一次面试中要求一个很有经验的嵌入式软件开发人员写出一个反转一段字符串并输出到屏 ...
- SQL Server审计功能入门:SQL Server审核 (SQL Server Audit)
原文:SQL Server审计功能入门:SQL Server审核 (SQL Server Audit) 介绍 Audit是SQL Server 2008之后才有的功能,它能告诉你"谁什么时候 ...
- hadoop工作平台梳理
文章 http://blog.csdn.net/lili72/article/details/41130743 lili72 数据平台: 一. hadoop平台:Hbase.hive,storm,s ...
- 坑爹BUG,没有详细的看还真看不出问题
Queue queue = new LinkedList<String> (); for(int i = 0; i<20; i++) { queue.add("坑爹&quo ...
- Controller 里面就只应该存放这些不能复用的代码(转)
MVC 的历史 MVC,全称是 Model View Controller,是模型 (model)-视图 (view)-控制器 (controller) 的缩写.它表示的是一种常见的客户端软件开发框架 ...
- 基于低压电力采集平台DW710C的基础开发
实验课题 (1)自己定义通信规约,採用java或C++编写简单的PC端上位机软件,实现採集器与PC机的通信.实验可在DW710C-PCproject下进行. (2)实现LCD显示字符.数字.汉字和简单 ...
- Leetcode 动态规划 Candy
本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie Candy Total Accepted: 16494 Total Submissions: ...
- 使用requirejs实现模块化编程
> 序言 - -# 公司大了,业务多了,前端代码量也逐渐增大,我们渐渐的依赖js实现的交互越来越多,长期以来会导致我们的代码维护越来越困难,所以依赖的插件也越来越多..比如这样页面中有大量的js ...
- Python installation
PIP:Python & OS Support pip works with CPython versions 2.6, 2.7, 3.2, 3.3, 3.4 and also pypy. p ...