ASP.NET Ajax 简单实例

本实例讲解Ajax 调用WCF服务。

1、建立一个网站,并在其中添加一个WCF服务(这里需要选择Ajax-Enabled WCF Service)。

2、IDE会自动生成一个SVC文件。

3、服务代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;

namespace WebApplication2
{
    [ServiceContract(Namespace = "WebApplication2")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Service1
    {
        // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
        // To create an operation that returns XML,
        //     add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
        //     and include the following line in the operation body:
        //         WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        [OperationContract]
        public bool ValidateUser(string uid, string pwd)
        {
            if(uid=="sa" && pwd=="sa")
            {
                return true;
            }
            return false;
        }
        //public void DoWork()
        //{
        //    // Add your operation implementation here
        //    return;
        //}

        // Add more operations here and mark them with [OperationContract]
    }
}

4、服务写好后就可以调用,这里在页面中添加一个ScriptManager,并引入WCF Webservice,代码如下
Get.aspx前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Get.aspx.cs" Inherits="WebApplication2.Get" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
     <script type="text/javascript">
         function ValidateUser(uid, pwd) {
             WebApplication2.Service1.ValidateUser(uid,pwd, OnSucceed, OnFailed);
         }

         function OnSucceed(result) {
             if (result == true) {
                 window.alert("通过验证");
             }
             else {
                 window.alert("验证失败!")
             }
         }

         function OnFailed(result) {
             window.alert("操作失败:" + result._message);
         }

         function Button1_onclick() {
             var uid = $get("tbxUid").value;
             var pwd = $get("tbxpwd").value;
             ValidateUser(uid,pwd);
         }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ScriptManager ID="scriptmanager1" runat="server">
    <Services>
    <asp:ServiceReference Path="~/Service1.svc" />
    </Services>
    </asp:ScriptManager>
    </div>
    <div>
    用户名:<input id="tbxUid" type="text" /><br/>
    密码:<input id="tbxpwd" type="text" /><br />
    <input id="button1" type="button" value="Submit" onclick="return Button1_onclick();"
    </div>
    </form>
   
</body>
</html>

5、这里需要注意的是,在调用 WebApplication2.Service1.ValidateUser方法时,代码中并没有直接取该函数的返回值,因为利用这种方案对服务器函数的调用都是异步的,正确的处理方法是指定了两个回调函数OnSucceed和OnFaile,第一个函数是成功时的回调,后一个是失败时的回调,这两个函数都需要一个参数,OnSucceed的参数就是服务器函数的返回值,而OnFaile的参数就是失败时的出差信息,功能类似Exception类型,其中_message属性中出错信息,_stackTrace中出错的堆栈跟踪信息。

6、这是一种常规的异步回调模式,大多数语言都会这么写。

7、运行结果:

原文地址:https://www.cnblogs.com/tylertang/p/3412191.html