<script language="javascript" type="text/javascript">
function doubleSalary()
{
var employee = new Object();
employee.FirstName = "X";
employee.LastName = "PP";
employee.Salary = 1000;
ComplexType.ServerMethod(employee, onServerMethodSucceeded);
}
function onServerMethodSucceeded(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);
}
</script>
这段代码首先定义了一个doubleSalary方法,里面定义了一个客户端employee实体类,然后将其传给服务器端WebService的某个方法。ComplexType 是WebService 的类名,ServerMethod是服务器端方法,onServerMethodSucceeded是客户端回调方法,作用是处理服务器方法结束之后的客户端事件,然后在这个事件中就能发现客户端之前定义的数据发生了变化
客户端在执行doubleSalary之后,发给服务器的RequestBody其实是下面的字符串:
{"employee":{"FirstName":"X","LastName":"PP","Salary":1000}}
而服务器响应回给客户端的数据其实是一段JSON字符串:
{"d":{"__type":"Employee","FirstName":"X","LastName":"PP","Salary":2000,"FullName":"X PP"}}
需要注意的是这里虽然是客户端调用服务器端的WebService,但并不是通过SOAP而是JSON来传送数据的