Jquery由于提供的$.ajax强大方法,使得其调用webservice实现异步变得简单起来,可以在页面上传递Json字符串到Webservice中,Webservice方法进行业务处理后,返回Json对象给页面,让页面去展现。
这一切都非常的简单,今天要学习的并非这些。我们在实际处理业务过程中,会发现往往页面要传递给webservice 的并非一个或多个字符串,有时候需要传递的是一个组合数据,如这样的一组数据:
{'Employee': [{'name':'John','sex':'man','age':'25'},{'name':'Tom','sex':'man','age':'21'}]}
客户端将这样的Json字符串作为$.ajax方法的data参数是没有问题的,然而,服务端的webservice该如何去写接收参数却成为了一个问题。在百度、谷歌了一番后,只发现提问的却没有回答的。索性还是自己去研究吧,发现其实Employee对象首先是一个数组,其次数组的每一项都是一个Dictionary<string,string>字典类型。于是我尝试在服务端使用Dictionary<string,string>[] Employee来接收客户端传递的参数,一切如我所料,成功!
客户端代码如下:
//JQuery 调用webService导入数据
function LoadData() {
var studentData = CollectionData();
$.ajax({
url: "ImportDataService.asmx/ImportStu",
type: "post",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: "{'students':[{'name':'KoBe ','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}",
success: function(result) {
alert(result.d);
},
error: function(e) {
alert(e.responseText);
}
});
}
function LoadData() {
var studentData = CollectionData();
$.ajax({
url: "ImportDataService.asmx/ImportStu",
type: "post",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: "{'students':[{'name':'KoBe ','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}",
success: function(result) {
alert(result.d);
},
error: function(e) {
alert(e.responseText);
}
});
}
//JQuery 调用webService导入数据
function LoadData() {
var studentData = CollectionData();
$.ajax({
url: "ImportDataService.asmx/ImportStu",
type: "post",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: "{'students':[{'name':'KoBe ','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}",
success: function(result) {
alert(result.d);
},
error: function(e) {
alert(e.responseText);
}
});
}
function LoadData() {
var studentData = CollectionData();
$.ajax({
url: "ImportDataService.asmx/ImportStu",
type: "post",
contentType: "application/json;charset=utf-8",
dataType: "json",
data: "{'students':[{'name':'KoBe ','sex':'boy','age':'20'},{'name':'Mary','sex':'girl','age':'19'}]}",
success: function(result) {
alert(result.d);
},
error: function(e) {
alert(e.responseText);
}
});
}
服务端代码如下:
///<summary>
///
///</summary>
///<param name="students"></param>
///<returns></returns>
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
publicstring ImportStu(Dictionary<string,string> []students)
{
if (students.Length ==0)
{
return"没有任何数据!";
}
else
{
try
{
foreach (Dictionary<string, string> stu in students)
{
//构造一个新的Student对象。
Student student =new Student();
//为新构造的Student对象属性赋值。
foreach (string key in stu.Keys)
{
switch (key)
{
case"name":
student.Name = stu[key];
break;
case"sex":
student.Sex = stu[key];
break;
case"age":
int age;
if (Int32.TryParse(stu[key], out age))
{
student.Age = age;
}
else
{
student.Age =0;
}
break;
default:
break;
}
}
}
return"导入学生成功!";
}
catch
{
thrownew Exception("导入学生失败!");
}
}
}
///
///</summary>
///<param name="students"></param>
///<returns></returns>
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
publicstring ImportStu(Dictionary<string,string> []students)
{
if (students.Length ==0)
{
return"没有任何数据!";
}
else
{
try
{
foreach (Dictionary<string, string> stu in students)
{
//构造一个新的Student对象。
Student student =new Student();
//为新构造的Student对象属性赋值。
foreach (string key in stu.Keys)
{
switch (key)
{
case"name":
student.Name = stu[key];
break;
case"sex":
student.Sex = stu[key];
break;
case"age":
int age;
if (Int32.TryParse(stu[key], out age))
{
student.Age = age;
}
else
{
student.Age =0;
}
break;
default:
break;
}
}
}
return"导入学生成功!";
}
catch
{
thrownew Exception("导入学生失败!");
}
}
}
///<summary>
///
///</summary>
///<param name="students"></param>
///<returns></returns>
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
publicstring ImportStu(Dictionary<string,string> []students)
{
if (students.Length ==0)
{
return"没有任何数据!";
}
else
{
try
{
foreach (Dictionary<string, string> stu in students)
{
//构造一个新的Student对象。
Student student =new Student();
//为新构造的Student对象属性赋值。
foreach (string key in stu.Keys)
{
switch (key)
{
case"name":
student.Name = stu[key];
break;
case"sex":
student.Sex = stu[key];
break;
case"age":
int age;
if (Int32.TryParse(stu[key], out age))
{
student.Age = age;
}
else
{
student.Age =0;
}
break;
default:
break;
}
}
}
return"导入学生成功!";
}
catch
{
thrownew Exception("导入学生失败!");
}
}
}
///
///</summary>
///<param name="students"></param>
///<returns></returns>
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
publicstring ImportStu(Dictionary<string,string> []students)
{
if (students.Length ==0)
{
return"没有任何数据!";
}
else
{
try
{
foreach (Dictionary<string, string> stu in students)
{
//构造一个新的Student对象。
Student student =new Student();
//为新构造的Student对象属性赋值。
foreach (string key in stu.Keys)
{
switch (key)
{
case"name":
student.Name = stu[key];
break;
case"sex":
student.Sex = stu[key];
break;
case"age":
int age;
if (Int32.TryParse(stu[key], out age))
{
student.Age = age;
}
else
{
student.Age =0;
}
break;
default:
break;
}
}
}
return"导入学生成功!";
}
catch
{
thrownew Exception("导入学生失败!");
}
}
}