本实例讲解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、运行结果:

ASP.NET Ajax 简单实例的更多相关文章

  1. PHP Ajax简单实例

    最近学习Jquery Ajax部分,通过简单例子,比较了下post,get方法的不同 HTML部分 <html> <head> <title>jQuery Ajax ...

  2. php的ajax简单实例

    很早就听闻ajax的名声,但是却一直不知道怎么用,今天自己捣鼓了一下,竟然会用了,哈哈哈哈. 为了防止我自己忘记,现在把这个简单的实例记录下.这个实例是网上搜的,文末附上链接. 首先你得有自己的服务器 ...

  3. ASP.NET Ajax简单的无刷新分页

    最近练习了一些AJAX无刷新分页,写得比较简单,性能不知道怎么样,求大神指点,如有更好的分页提供,欢迎交流! 发话不多说了,直接上代码! 首先从网上下了一个JS分页,感觉挺好用的 (function( ...

  4. Ajax 简单实例,其实就是js里面内容有些不同而已(转载)

    这些时间,瞎子也看得见,AJAX正大踏步的朝我们走来.不管我们是拥护也好,反对也罢,还是视而不见,AJAX像一阵潮流,席转了我们所有的人. 关于AJAX的定义也好,大话也好,早有人在网上发表了汗牛充栋 ...

  5. EXT ajax简单实例

    转载:http://www.cnblogs.com/xiepeixing/archive/2012/10/24/2736751.html EXT ajax request是ext中对于ajax请求的实 ...

  6. AJAX简单实例

    越用AJAX越觉得它的强大.好用. 平常我们提交表单,是直接通过action属性,直接向后台提交数据. 我们也可以用AJAX向后台提交数据.例如: 这是一个表单,两个字段:notice,scort,保 ...

  7. ajax 简单实例

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script> ...

  8. Ajax简单实例(基于jQuery)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  9. asp.net ajax 简单案例

    第一步先引用 scriptManager <asp:UpdatePanel ID="UpdatePanelGuanZhu" runat="server"& ...

随机推荐

  1. 学习javascript系列之变量

    在javascript全局变量中,未加var声明的全局变量和加上var声明的全局变量是不同的,虽然都是window对象的属性. ; window.a //1 delete a //false; 通过v ...

  2. [KOJ6024]合并果子·改(强化版)

    [COJ6024]合并果子·改(强化版) 试题描述 在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆.多多把这些果子堆排成一排,然后所有的果子合成一堆.    每一次合并 ...

  3. Android 中 SQLite 数据库的查看

    当 SQLite 数据库创建完成后,如何查看数据库的内容呢?如果直接使用 File Explorer 查看,最多只能看到 database 目录下出现了一个 BookStore.db 文件,Book ...

  4. Apache Thrift 环境配置

    在 Ubuntu 14.04 下Apache Thrift 的安装方法: 1安装依赖包 sudo apt-get install libboost-dev libboost-test-dev libb ...

  5. 7.6--找过点最多的直线(CC150)

    直接两个点确定一条直线.然后两两组合,再写一个看过多少个点的函数.一直更新max就行. import java.util.Arrays; public class Solution { public ...

  6. Fibonacci 1

    Fibonacci 1 题面 \[F_0=0,F_1=1,F_n=F_{n-1}+F_{n-2}\] 给定\(n\),求 \[S(n)=\sum_{i=1}^{n}F_nF_{n-1}\] 数据格式 ...

  7. jquery学习——遍历

    1.each() $(selector).each(function(index,element)) var arr = [ "a", "bb", " ...

  8. 7.31 签到,js 全局预处理笔记

    js 解析与执行过程: 一.全局:  1.预处理阶段 : 1.LexicalEnviroment === window {1.预处理 var   |   2.function xxx //预处理申明的 ...

  9. centos7下搭建git和gitlab版本库

    系统:centos7 安装依赖: yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-d ...

  10. centos7安装docker并设置开机启动

    版本要求:查看内核版本,需大于3.10 [root@localhost ~]# uname -r -.el7.x86_64 更新内核:如果是生产机器务必慎重更新内核,避免出现不必要的问题. sudo ...