62,267
社区成员
发帖
与我相关
我的任务
分享using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
/// <summary>
/// WebService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
public WebService()
{
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public Employee GetEmployeeOne()
{
return new Employee("beniao", "boy", "22");
}
[WebMethod]
public Employee GetEmployeeTwo(Employee employee)
{
return new Employee(employee.Name, employee.Sex, employee.Age);
}
//[WebMethod]
// 这里该怎么定义接收二维数组的参数?????
//public Employee GetEmployee(string []arr)
//{
// return new Employee(arr[0] arr[0].Sex, arr[0].Age);
//}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function GetEmployeeOne()
{
WebService.GetEmployeeOne(CallBack);
}
function GetEmployeeTwo()
{
//使用JSON进行传输
var employee = {"Name":"Yuang", "Sex":"girl", "Age":"21"};
WebService.GetEmployeeTwo(employee , CallBack);
}
function GetEmployee()
{
//使用JSON进行传输
var employee = [{"Name":"Yuang", "Sex":"girl", "Age":"21"},{"Name":"zz", "Sex":"boy", "Age":"88"}];
WebService.GetEmployee(employee , CallBack);
}
//回调函数
function CallBack(employee)
{
var obj=document.getElementById("Result");
obj.innerHTML=String.format("Name:{0} Sex:{1} Age:{2}",
employee.Name,
employee.Sex,
employee.Age);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager2" runat="server">
<Services>
<asp:ServiceReference Path="WebService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
</div>
<input id="Button1" type="button" value="无参数交互" onclick="GetEmployeeOne();" />
<input id="Button2" type="button" value="带参数交互" onclick="GetEmployeeTwo();" />
<input id="Button3" type="button" value="测试" onclick="GetEmployee();" />
<br /><br />
<div id="Result" style="width:230px;height:30px;font-weight:bold;font-size:medium;background-color:#CCC;"></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Class1 的摘要说明
/// </summary>
public class Employee
{
/**//// <summary>
/// 作为参数的类型一定要有默认的构造函数
/// </summary>
public Employee()
{
}
public Employee(string name, string sex, string age)
{
this.Name = name;
this.Sex = sex;
this.Age = age;
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _sex;
public string Sex
{
get { return _sex; }
set { _sex = value; }
}
private string _age;
public string Age
{
get { return _age; }
set { _age = value; }
}
}
function GetEmployee()
{
//使用JSON进行传输
var employee = [{"Name":"Yuang", "Sex":"girl", "Age":"21"},{"Name":"zz", "Sex":"boy", "Age":"88"}];
WebService.GetEmployee(employee , CallBack);
}
//回调函数
function CallBack(employee)
{
var obj=document.getElementById("Result");
obj.innerHTML=String.format("Name:{0} Sex:{1} Age:{2}",
employee.Name,
employee.Sex,
employee.Age);
}
[WebMethod]
public Employee GetEmployee(string []arr)
{
//return new Employee(arr[0], arr[0].Sex, arr[0].Age);
}