惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
B
Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
B
Blog RSS Feed
Scott Helme
Scott Helme
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
W
WeLiveSecurity

博客园 - DreamTrue

发布新浪微博API SDK,附Demo地址!! NHibernate N+1问题实例分析和优化 可以防止重复提交的问题 - DreamTrue - 博客园 JavaScript 乘法bug及格式化小数位数 验证电话,手机,小灵通较正确的正则表达式 RegisterClientScriptBlock失效的问题 - DreamTrue - 博客园 程序员朋友们,注意保护眼睛啊!! 基础知识补遗-短路运算符和非短路运算符 用Response将字符串输出到文本(弹出文件下载框) - DreamTrue - 博客园 关于应用WCF X.509证书验证的小结 终于看完了《WCF服务编程》 什么是有状态的服务? 谁能帮我解答《WCF服务编程》第4章实例管理的疑问? 准备购买新书 ADO.Net EF Start (3):实体数据模型(MSDN)EDM1.0版 ADO.Net EF Start (2):Overview asp.net页面防止按钮重复提交的小技巧 ADO.Net EF Start (1):ADO.NET 实体框架概述(MSDN) Asp.net Ajax【客户端二】Sys.Net.WebRequest
Asp.net Ajax【客户端三】异步调用
DreamTrue · 2008-04-25 · via 博客园 - DreamTrue

异步调用分两部分:异步调用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元

image

输入:Name=天天,Pay=34

输出:错误: 天天,对不起,实在找不到低于10000元的白菜价汽车

image

二.异步调用页面方法

页面方法继续沿用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....) // 此时会调用默认方法