//前台代码
    <form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ComplexType.asmx" />
</Services>
</asp:ScriptManager>
        
<input type="button" value="Double Salary" onclick="doubleSalary()" />
<input type="button" value="Reverse" onclick="reverse([1, 2, 3, 4, 5])" />
<input type="button" value="Get Employees" onclick="getEmployees()" />
        
<script language="javascript" type="text/javascript">
//传递对象
function doubleSalary()
{
var employee = new Object();
employee.FirstName = "Jeffrey";
employee.LastName = "Zhao";
employee.Salary = 1000;
                
ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
}
function doubleSalarySucceeded(result)
{
var message = String.format(
"First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
result.FirstName,
result.LastName,
result.FullName,
result.Salary);
                    
alert(message);
}
//数组倒序
function reverse(array)
{
ComplexType.Reverse(array, function(result){alert(result);});
}
//获取对象数组
function getEmployees()
{
ComplexType.GetEmployees(getEmployeesSucceeded);
}
            
function getEmployeesSucceeded(result)
{
for (var name in result)
{
alert(name + ": " + result[name].Salary)
}
}
            
</script>
</form>
//WebService<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ComplexType.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" value="Double Salary" onclick="doubleSalary()" />
<input type="button" value="Reverse" onclick="reverse([1, 2, 3, 4, 5])" />
<input type="button" value="Get Employees" onclick="getEmployees()" />
<script language="javascript" type="text/javascript">
//传递对象
function doubleSalary()
{
var employee = new Object();
employee.FirstName = "Jeffrey";
employee.LastName = "Zhao";
employee.Salary = 1000;
ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
}
function doubleSalarySucceeded(result)
{
var message = String.format(
"First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
result.FirstName,
result.LastName,
result.FullName,
result.Salary);
alert(message);
}
//数组倒序
function reverse(array)
{
ComplexType.Reverse(array, function(result){alert(result);});
}
//获取对象数组
function getEmployees()
{
ComplexType.GetEmployees(getEmployeesSucceeded);
}
function getEmployeesSucceeded(result)
{
for (var name in result)
{
alert(name + ": " + result[name].Salary)
}
}
</script>
</form>
    [WebMethod]
public Employee DoubleSalary(Employee employee)
{
employee.Salary *= 2;
return employee;
}
[WebMethod]
public List<int> Reverse(List<int> list)
{
list.Reverse();
return list;
}
[WebMethod]
public IDictionary<string, Employee> GetEmployees()
{
Dictionary<string, Employee> result = new Dictionary<string, Employee>();
Employee emp1 = new Employee();
emp1.FirstName = "Jeffrey";
emp1.LastName = "Zhao";
emp1.Salary = 1000;
result[emp1.FullName] = emp1;
Employee emp2 = new Employee();
emp2.FirstName = "Tom";
emp2.LastName = "Chen";
emp2.Salary = 2000;
result[emp2.FullName] = emp2;
return result;
}
//实体类public Employee DoubleSalary(Employee employee)
{
employee.Salary *= 2;
return employee;
}
[WebMethod]
public List<int> Reverse(List<int> list)
{
list.Reverse();
return list;
}
[WebMethod]
public IDictionary<string, Employee> GetEmployees()
{
Dictionary<string, Employee> result = new Dictionary<string, Employee>();
Employee emp1 = new Employee();
emp1.FirstName = "Jeffrey";
emp1.LastName = "Zhao";
emp1.Salary = 1000;
result[emp1.FullName] = emp1;
Employee emp2 = new Employee();
emp2.FirstName = "Tom";
emp2.LastName = "Chen";
emp2.Salary = 2000;
result[emp2.FullName] = emp2;
return result;
}
public class Employee
{
public string FirstName;
public string LastName;
public int Salary;
public string FullName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
}
{
public string FirstName;
public string LastName;
public int Salary;
public string FullName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
}
                    
                
                
            
        
浙公网安备 33010602011771号