Asp.net Ajax【客户端三】异步调用
异步调用分两部分:异步调用Web服务,异步调用页面代码
一.异步调用Web服务
web服务代码
[WebService(Namespace = "http://tempuri.org/
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
[GenerateScriptType(typeof(CarInfo))]
public class InnerWs : System.Web.Services.WebService {
public InnerWs () {
//如果使用设计的组件,请取消注释以下行
//InitializeComponent();
}
[WebMethod]
public CarInfo GetCarByPayment(int cost)
{
CarInfo car=new CarInfo();
if( cost <= 10000) throw new NotSupportedException("对不起,实在找不到低于10000元的白菜价汽车");
if( cost >10000 && cost <= 50000) car = new CarInfo("奇瑞", "QQ", 22000);
if( cost >50000 && cost <= 80000) car = new CarInfo("铃木", "标准", 76400);
if( cost >80000 && cost <= 130000) car = new CarInfo("大众", "Polo", 110400);
if( cost >130000 && cost <= 22000) car = new CarInfo("大众", "3000", 191000);
if( cost > 220000 && cost <= 340000) car = new CarInfo("大众", "帕萨特", 276200);
if( cost >340000 && cost <= 500000) car = new CarInfo("宝马", "5i", 389000);
if( cost >500000 && cost <= 700000) car = new CarInfo("宝马", "7i", 623000);
if( cost >700000 && cost <= 900000) car = new CarInfo("奔驰", "豪华", 888000);
if( cost >900000 && cost <=1500000) car = new CarInfo("保时捷", "911GT", 1370000);
if( cost > 1500000 && cost <= 2500000) car = new CarInfo("法拉利", "511", 2200000);
if( cost > 2500000 && cost <= 3500000) car = new CarInfo("劳斯莱斯", "经典防弹", 3400000);
if( cost > 3500000) throw new NotSupportedException("你钱多的没处花,建议你去找个女友");
return car;
}
}
CarInfo类
public class CarInfo
{
public CarInfo()
{}
public CarInfo(string brand, string type, int price)
{
Brand = brand;
Type = type;
Price = price;
}
public string Brand { get; set; }
public string Type { get; set; }
public int Price { get; set; }
}
页面调用代码
<script type="text/javascript">
function CompleteCallBack(result,context)
{
var carObj=new CarInfo();
carObj=result;
Sys.Debug.traceDump(String.format("{0},与你出价接近的车是:{1} {2} 系列,价格是:¥{3}元 ",context,carObj.Brand,carObj.Type,carObj.Price),"成功");
}
function ErrorCallBack(error,context)
{
Sys.Debug.traceDump(context+","+error.get_message(),"错误");
}
function Button1_onclick() {
var cost=$get("costBox").value;
if($get("name").value=="")
{
alert("请输入你的名字");
return;
}
if(!isNaN(cost))
{
if(cost<1||cost>9999999)
alert("值必须介于1-9999999之间");
else
{
Sys.Debug.clearTrace();
InnerWs.GetCarByPayment(cost,CompleteCallBack,ErrorCallBack,$get("name").value);
}
}
else
alert("必须为数字");
}
</script>
<style type="text/css">
#costBox
{
width: 188px;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/InnerWs.asmx" />
</Services>
</asp:ScriptManager>
</div>
How much would you pay for your new car?
<br />
Name:<input id="name" type="text" /> You pay=<input id="costBox" type="text" /><input id="Button1" type="button" value="See what can i buy"
onclick="return Button1_onclick()" /><br />
<br />
<fieldset title="输出" style="width: 608px">
<legend>输出结果</legend>
<textarea id="TraceConsole" style="width: 602px; height: 29px;"
readonly="readonly" rows="1"></textarea>
</fieldset>
</form>
</body>
</html>
运行结果:
输入:Name=天天,Pay=345678
输出:成功: 天天,与你出价接近的车是:宝马 5i 系列,价格是:¥389000元
输入:Name=天天,Pay=34
输出:错误: 天天,对不起,实在找不到低于10000元的白菜价汽车
二.异步调用页面方法
页面方法继续沿用Web服务的方法内容,只是签名和属性不同
[WebMethod]
[GenerateScriptType(typeof(CarInfo))]
public static CarInfo GetCarByPayment(int cost)
页面调用时,首先要把ScriptManager设为
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
然后,是把调用方法的方式改为
PageMethods.GetCarByPayment(cost,CompleteCallBack,ErrorCallBack,$get("name").value);
除上述两点,其他与Web服务的页面调用相同,输出结果当然也相同。
体会
1.异步回调方法和错误回调方法的完整签名是
function MethodName (param1,param2,...paramN,callbackfunction,errorfunction,context)
context是上下文参数,可以是任意类型
2.对于异步返回的是复杂对象,需要用GenerateScriptType标签(调用页面方法也需要设置),指定其需要序列化的类型,默认是JSON序列化
3.可以先设置异步Web服务代理,并设置其默认回调和默认错误等默认方法,然后再调用该代理
如:var proxy=new Namespace.ClassName();
proxy.set_defaultSucceededCallback(defaultSucceededCallback);
proxy.MethodName(param1,param2....) // 此时会调用默认方法