12 WCF与Ajax编程

Ajax

    Ajax基本原理

  AJAX技术的本质原理就是:使用 JavaScript 的 XMLHttpRequest 对象来直接与服务器进行通信。

  通过这个对象,JavaScript 可在不重载页面的情况与 Web 服务器交换数据。然后通过DOM更新部分页面数据。

  Ajax最大的优点

      在于它带来的更好的用户体验。传统的web页面请求不同,AJAX技术当提交表单时就向web服务器发送一个请求。

    服务器接收并处理传来的表单,然後返回一个新的网页。这个做法浪费了许多带宽,

    因为在前後两个页面中的大部分HTML代码往往是相同的。AJAX 在浏览器与 Web 服务器之间使用异步数据传输(HTTP 请求),

    这样就可使网页从服务器请求少量的信息,而不是整个页面。

ajax调用WCF 

    WCF服务,为了能使js调用,必须为服务实现类(因为AspNetCompatibilityRequirements只对类有效)设置AspNetCompatibilityRequirements为Allowed,使得

  WCF跟AspNet的技术相兼容。

    WCF虽然在设计上可以进行独立传输,但当应用于一个ASP.NET AJAX应用程序环境下时,

  WCF服务实际上可以工作在一种十分类似于ASMX服务的方式下。

  借助于这个AspNetCompatibilityRequirements属性,我们可以指示WCF使用与ASMX服务相同的模型进行工作。

  

  注意,要实现WCF服务与ASMX服务的兼容性至少要实现两点。

    第一,在配置文件web.config中,需要进行类似如下的声明式定义:

<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

   第二,开发者需要显式地选择一个给定WCF服务的兼容性方式,这是通过使用服务AspNetCompatibilityRequirements属性完成的。

新建一个WCF应用程序项目:

IServer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Security.Cryptography.X509Certificates;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text; namespace Keasy5.WCF.Ajax.Service
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(string value);
}
}

Service1.svc.cs

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;
using Keasy5.WCF.Ajax.Service; namespace Keasy5.WCF.Ajax.Service
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("Hi:{0}", value);
}
}
}

服务端配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration> <system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors> <endpointBehaviors>
<behavior name="enableWebScript_ajax">
<enableWebScript />
</behavior>
</endpointBehaviors> </behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"
aspNetCompatibilityEnabled="true"/> <services>
<service name="Keasy5.WCF.Ajax.Service.Service1">
<endpoint address=""
behaviorConfiguration="enableWebScript_ajax"
binding="webHttpBinding"
contract="Keasy5.WCF.Ajax.Service.IService1"></endpoint>
</service>
</services> </system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer> </configuration>

  右键Service1.svc--在浏览器中查看,在原来的地址中加入“/js”,修改地址为:

    http://localhost:18476/Service1.svc/js

  WCF 服务返回:

Type.registerNamespace('tempuri.org');
tempuri.org.IService1=function() {
tempuri.org.IService1.initializeBase(this);
this._timeout = ;
this._userContext = null;
this._succeeded = null;
this._failed = null;
}
tempuri.org.IService1.prototype={
_get_path:function() {
var p = this.get_path();
if (p) return p;
else return tempuri.org.IService1._staticInstance.get_path();},
GetData:function(value,succeededCallback, failedCallback, userContext) {
return this._invoke(this._get_path(), 'GetData',false,{value:value},succeededCallback,failedCallback,userContext); }}
tempuri.org.IService1.registerClass('tempuri.org.IService1',Sys.Net.WebServiceProxy);
tempuri.org.IService1._staticInstance = new tempuri.org.IService1();
tempuri.org.IService1.set_path = function(value) { tempuri.org.IService1._staticInstance.set_path(value); }
tempuri.org.IService1.get_path = function() { return tempuri.org.IService1._staticInstance.get_path(); }
tempuri.org.IService1.set_timeout = function(value) { tempuri.org.IService1._staticInstance.set_timeout(value); }
tempuri.org.IService1.get_timeout = function() { return tempuri.org.IService1._staticInstance.get_timeout(); }
tempuri.org.IService1.set_defaultUserContext = function(value) { tempuri.org.IService1._staticInstance.set_defaultUserContext(value); }
tempuri.org.IService1.get_defaultUserContext = function() { return tempuri.org.IService1._staticInstance.get_defaultUserContext(); }
tempuri.org.IService1.set_defaultSucceededCallback = function(value) { tempuri.org.IService1._staticInstance.set_defaultSucceededCallback(value); }
tempuri.org.IService1.get_defaultSucceededCallback = function() { return tempuri.org.IService1._staticInstance.get_defaultSucceededCallback(); }
tempuri.org.IService1.set_defaultFailedCallback = function(value) { tempuri.org.IService1._staticInstance.set_defaultFailedCallback(value); }
tempuri.org.IService1.get_defaultFailedCallback = function() { return tempuri.org.IService1._staticInstance.get_defaultFailedCallback(); }
tempuri.org.IService1.set_enableJsonp = function(value) { tempuri.org.IService1._staticInstance.set_enableJsonp(value); }
tempuri.org.IService1.get_enableJsonp = function() { return tempuri.org.IService1._staticInstance.get_enableJsonp(); }
tempuri.org.IService1.set_jsonpCallbackParameter = function(value) { tempuri.org.IService1._staticInstance.set_jsonpCallbackParameter(value); }
tempuri.org.IService1.get_jsonpCallbackParameter = function() { return tempuri.org.IService1._staticInstance.get_jsonpCallbackParameter(); }
tempuri.org.IService1.set_path("http://localhost:18476/Service1.svc");
tempuri.org.IService1.GetData= function(value,onSuccess,onFailed,userContext) {tempuri.org.IService1._staticInstance.GetData(value,onSuccess,onFailed,userContext); }

  可以看成是服务端调用的代理类。

示例1:用户验证

示例2:滚动图片新闻

【The end】

【WCF--初入江湖】12 WCF与Ajax编程的更多相关文章

  1. WCF分布式开发步步为赢(12):WCF事务机制(Transaction)和分布式事务编程

    今天我们继续学习WCF分布式开发步步为赢系列的12节:WCF事务机制(Transaction)和分布式事务编程.众所周知,应用系统开发过程中,事务是一个重要的概念.它是保证数据与服务可靠性的重要机制. ...

  2. WCF分布式开发步步为赢(4):WCF服务可靠性传输配置与编程开发

    今天继续WCF分布式开发步步为赢系列的第4节:WCF服务可靠性传输配置与编程开发.这个章节,我们要介绍什么是WCF服务的可靠性传输,随便介绍网络协议的概念,Web Service为什么不支持可靠性传出 ...

  3. http服务&ajax编程

    http服务&ajax编程 1.服务器 前言:通俗的讲,能够提供某种服务的机器(计算机)称为服务器 1.1.服务器类型 按照不同的划分标准,服务可划分为以下类型: 按服务类型可分为:文件服务器 ...

  4. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful)

    近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只考虑dotnet到dotnet的情形下,我们可以选择remoting.WCF(http).WCF(tcp).WCF(RESTful). ...

  5. 大比速:remoting、WCF(http)、WCF(tcp)、WCF(RESTful)、asp.net core(RESTful) .net core 控制台程序使用依赖注入(Autofac)

    大比速:remoting.WCF(http).WCF(tcp).WCF(RESTful).asp.net core(RESTful) 近来在考虑一个服务选型,dotnet提供了众多的远程服务形式.在只 ...

  6. 01HTTP服务&AJAX编程

    HTTP服务&AJAX编程 一.服务器         1. 什么是服务器? 能够提供某种服务的机器(计算机)称为服务器. 2.服务器的分类:              1.按系统分类:Lin ...

  7. WCF服务与WCF数据服务的区别

    问: Hi, I am newbie to wcf programming and a little bit confused between WCF Service and WCF Data  Se ...

  8. AJAX编程-封装ajax工具函数

    即 Asynchronous [e'sɪŋkrənəs] Javascript And XML,AJAX 不是一门的新的语言,而是对现有技术的综合利用.本质是在HTTP协议的基础上以异步的方式与服务器 ...

  9. Ajax编程中,经常要能动态的改变界面元素的样式

    在Ajax编程中,经常要能动态的改变界面元素的样式,可以通过对象的style属性来改变,比如要改变背景色为红色,可以这样写:element.style.backgroundColor=”#ff0000 ...

随机推荐

  1. a标签替代input的submit提交功能

    在工作中有时候会遇到A标签,但是提交表单的时候我们需要用到submit来提交表单,下面几行代码很好的解决了这个问题! <div class="btn"><a hr ...

  2. UNIX 信号基本概念

    1. 信号的基本概念 为了理解信号,先从我们最熟悉的场景说起: 用户输入命令,在Shell下启动一个前台进程. 用户按下Ctrl-C,这个键盘输入产生一个硬件中断. 如果CPU当前正在执行这个进程的代 ...

  3. Ajax 技术一

    一.Ajax概述 1.历史起源 1998年,微软公司Outlook Web Access研发小组在当时的IE浏览器中集成了一种技术,可以在客户端无刷新的前提下向服务器端发送Http请求,这门技术称之为 ...

  4. mysql之触发器trigger(1)

    触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...

  5. 使用DbVisualizer 8 连接Oracle数据库

    1. 网上下载一个驱动包ojdbc14.jar,放到oracle目录下:...\DbVisualizer-8.0.1\jdbc\oracle\ojdbc14.jar 2. 打开 DbVisualize ...

  6. 数值积分NIntegrate中的具体算法

    数值积分方法很多,Mathematica中至提供了NIntegrate.具体算法可参照官方帮助. http://reference.wolfram.com/language/tutorial/NInt ...

  7. uml的关联多重度

    UML中关联的多重度是指一个类的实例能够与另一个类的多少个实例相关联,这个“多少”被称为关联角色的多重度指定关联一端的多重度.也可以这样理解:在关联另一端的类的每个对象要求在本端的类必须有多 少个对象 ...

  8. NSS_10 EXTJS给弹出的子窗口传递参数

    在桌面程序中, 如果需要弹出一个子面板, 并且需要传一些参数给子面板, 我通常的作法就是:在子面板添加对应的数据成员,然后一个构造函数来接收这些参数并赋值级数据成员. 实现起来非常方便. 但是在Ext ...

  9. 【Sql Server】使用触发器把一个表中满足条件的数据部分字段插入到另一个表中

    create trigger 触发器名称 on 对哪个表起作用 after insert,update as return set nocount on begin transaction; inse ...

  10. PHP+AJAX无刷新返回天气预报

    AjaxJavaScript天气预报php天气预报,用php来写一个天气预报的模块. 天气数据是通过采集中国气象网站的.本来中国天气网站也给出了数据的API接口.以下是API的地址.返回的数据格式为j ...