WCF 第十三章 可编程站点 使用AJAX和JSON进行网页编程
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;namespace EssentialWCF
{[ServiceContract(Namespace = "EssentialWCF")][ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class GameReviewService{private string[] gamelist = new string[] { "Viva Pinata", "Star Wars Lego", "Spiderman Ultimate","Gears of War", "Halo 2", "Halo 3"};private Dictionary<string, List<string>> reviews;public GameReviewService(){reviews = new Dictionary<string, List<string>>();foreach (string game in gamelist){reviews.Add(game, new List<string>());}}[OperationContract][WebGet]public string[] Games(){return gamelist;}[OperationContract][WebGet]public string[] Reviews(string game){WebOperationContext ctx = WebOperationContext.Current;ctx.OutgoingResponse.Headers.Add("Cache-Control", "no-cache");if (!reviews.ContainsKey(game)){return null;}List<string> listOfReviews = reviews[game];if (listOfReviews.Count == 0){return new string[] { string.Format("No reviews found for {0}.", game) };}else{return listOfReviews.ToArray();}}[OperationContract][WebInvoke]public void AddReview(string game, string comment){reviews[game].Add(comment);}[OperationContract][WebInvoke]public void ClearReviews(string game){reviews[game].Clear();}}
}
<%@ ServiceHost Language="C#" Debug="true" Service="EssentialWCF.GameReviewService"
CodeBehind="GameReviewService.cs" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
<system.serviceModel><serviceHostingEnvironment aspNetCompatibilityEnabled="true" /><services><service behaviorConfiguration="MetadataBehavior" name="EssentialWCF.GameReviewService"><endpoint address="" binding="webHttpBinding" contract="EssentialWCF.GameReviewService" /><endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" /></service></services><behaviors><endpointBehaviors><behavior name="AjaxBehavior"><enableWebScript /></behavior><behavior name="XBOX360GameReview.Service1AspNetAjaxBehavior"><enableWebScript /></behavior></endpointBehaviors><serviceBehaviors><behavior name="MetadataBehavior"><serviceMetadata httpGetEnabled="true" /><serviceDebug includeExceptionDetailInFaults="false" /></behavior></serviceBehaviors></behaviors></system.serviceModel>
<asp:ScriptManager ID="ScriptManager1" runat="server"><Services><asp:ServiceReference Path="~/GameReviewService.svc"/></Services></asp:ScriptManager>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="EssentialWCF._Default" %><%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!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 id="Head1" runat="server"><title>XBOX 360 Game Reviews</title><script type="text/javascript">function pageLoad() {}</script>
</head>
<body><form id="form1" runat="server"><div><asp:ScriptManager ID="ScriptManager1" runat="server"><Services><asp:ServiceReference Path="~/GameReviewService.svc"/></Services></asp:ScriptManager><script type="text/javascript">EssentialWCF.GameReviewService.set_defaultFailedCallback(OnError);function ListGames() {EssentialWCF.GameReviewService.Games(OnListGamesComplete);}function ListReviews() {var gameListBox = document.getElementById("GameListBox");EssentialWCF.GameReviewService.Reviews(gameListBox.value, OnListReviewsComplete);}function AddReview() {var gameListBox = document.getElementById("GameListBox");var reviewTextBox = document.getElementById("ReviewTextBox");EssentialWCF.GameReviewService.AddReview(gameListBox.value, reviewTextBox.value, OnUpdateReviews);}function ClearReviews() {var gameListBox = document.getElementById("GameListBox");EssentialWCF.GameReviewService.ClearReviews(gameListBox.value, OnUpdateReviews);}function OnListGamesComplete(result) {var gameListBox = document.getElementById("GameListBox");ClearAndSetListBoxItems(gameListBox, result);}function OnListReviewsComplete(result) {var reviewListBox = document.getElementById("ReviewListBox");ClearAndSetListBoxItems(reviewListBox, result);}function OnUpdateReviews(result) {ListReivews();}function ClearAndSetListBoxItems(listBox, games) {for (var i = listBox.options.length - 1; i > -1; i--) {listBox.options[i] = null;}var textValue;var optionItem;for (var j = 0; j < games.length; j++) {textValue = games[j];optionItem = new Option(textValue, textValue, false, false);listBox.options[listBox.length] = optionItem;}}function OnError(result) {alert("Error: " + result.get_message());}function OnLoad() {ListGames();var gameListBox = document.getElementById("GameListBox");if (gameListBox.attachEvent) {gameListBox.attachEvent("onchange", ListReviews);}else {gameListBox.attachEventListener("change", ListReviews, false);}}Sys.Application.add_load(OnLoad);</script><h1>XBOX 360 Game Review</h1><table><tr style="height:250px;vertical-align:top;"><td style="width:240px">Select a game:<br /><asp:ListBoxID="GameListBox" runat="server"Width="100%"></asp:ListBox></td><td style="width:400px">Commnets:<br /><asp:ListBoxID="ReviewListBox" runat="server" Width="100%"Height="100%"></asp:ListBox></td></tr><tr style="vertical-align:top;"><td colspan="2">Enter a comment:<br/><asp:TextBox ID="ReviewTextBox" runat="server"Width="400px"></asp:TextBox><input id="AddReviewButton" type="button" value="Add" οnclick="AddReview();" /><input id="ClearReviewButtion" type="button" value="Clear" οnclick="ClearReviews();" /></tr></table></div></form>
</body>
</html>
WCF 第十三章 可编程站点 使用AJAX和JSON进行网页编程相关推荐
- WCF 第十三章 可编程站点 所有都与URI相关
普及的GET 方法 表13.1中所有URIs的一件共性的事情是它们都是用HTTP协议来访问资源.HTTP协议被认为是站点协议.HTTP协议的初衷是交换HTML页,但是它已经被用来访问所有类型的资源,包 ...
- WCF 第十三章 可编程站点 使用WebGet和WebInvoke
服务可以使用WebHttpBinding以及WebGet或者WebInvoke属性来暴露.这些属性每一个都确定HTTP动作.消息格式以及需要暴露给一个操作的消息体形式.我们将检查这些属性的每一个并给出 ...
- 第二十三章 天猫精灵控制ESP32(wifi+tcp+json)
关注嘉友创科技公众号 源码地址:https://github.com/HX-IoT/ESP32-Developer-Guide ESP32开发指南QQ群:824870185,内有pdf版,排版整洁. ...
- [转]Windows Shell 编程 第十三章 【来源:http://blog.csdn.net/wangqiulin123456/article/details/7988004】...
第十三章 Windows脚本环境 现在的许多开发人员以前都是在MS-DOS环境下编程的.几乎所有人都接触过批处理文件--一种基于文本命令的文件.这种文件使你能够在一个可执行命令中组合多个指令.批处理文 ...
- 《Java编程思想》读书笔记 第十三章 字符串
<Java编程思想>读书笔记 第十三章 字符串 不可变String String对象是不可变的,每一个看起来会修改String值的方法,实际上都是创建一个全新的String对象,以及包含修 ...
- 第十三章 Perl的面向对象编程
第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介 二.Perl中的类 三.创建类 四.构造函数 实例变量 五.方法 六.方法的输出 七.方法的调用 八.重载 九.析构函数 ...
- 20190825 On Java8 第十三章 函数式编程
第十三章 函数式编程 函数式编程语言操纵代码片段就像操作数据一样容易. 虽然 Java 不是函数式语言,但 Java 8 Lambda 表达式和方法引用 (Method References) 允许你 ...
- 【正点原子Linux连载】第二十三章 音频应用编程-摘自【正点原子】I.MX6U嵌入式Linux C应用编程指南V1.1
第二十三章 音频应用编程 ALPHA I.MX6U开发板支持音频,板上搭载了音频编解码芯片WM8960,支持播放以及录音功能! 本章我们来学习Linux下的音频应用编程,音频应用编程相比于前面几个章节 ...
- 高级shell编程笔记(第三十三章 杂项)
第三十三章 杂项 33.1 交互式和非交互式的shell和脚本 交互式的shell在tty终端从用户的输入中读取命令.另一方面,shell能在启动时读取启动文件,显示一个提示符并默认激活作业控制.用户 ...
最新文章
- 抢红包算法 c语言,红包分配算法,抢红包算法
- 人工智能,也许是另一场文艺复兴!
- linux同名文件夹覆盖_第一天:Linux——复习
- APK文件的解包打包和修改
- 大数据技术 学习之旅_数据-数据科学之旅的起点
- 中心城镇问题(长链剖分优化树形dp)
- 嵌入式基于Linux电机控制,基于嵌入式arm+linux平台的直流电机调速控制系统.pdf
- js基础--变量、数据类型、循环、判断、函数定义
- 使用GridFsTemplate来实现文件归类
- SQLI-LABS(二)
- 取消c++所设置的cout中setprecision输出的格式
- 华为MA5616配置及维护指南
- Datawhale 零基础入门数据挖掘-Task4 建模调参
- 截取usb数据包,控制usb设备----Relay设备
- 可局部放大图片的例子,局部放大图片,javascript局部放大图片
- 两段检验系统生成的identityHashCode是否重复的代码
- ORACLE AWR报告生成过程出现多个实例记录分析
- regsvr32注册Dll文件时出现0x80004005问题的解决方案
- 找到Java中长整型(long)所能表示的最大素数: 9223372036854775783
- AtCoder Beginner Contest 275 A-D题解
热门文章
- TCP协议(标志位URG、PSH,定时器,连接的建立和断开)
- pytest测试框架(五)---使用xfail将用例标记为失败
- 数据库-Windows启动和停止mysql
- 教你玩转vue-router命令视图
- mysql抓取csv_如何将.csv数据上传、提取、存储到服务器并传输到mysql
- msdn服务器系统,操作系统
- java的jtextarea_java中怎么在JTextArea中添加图片?
- java list 遍历查找_Java用list储存,遍历,查询指定信息过程详解
- 知道圆心半径取圆上的经纬度_作为一名台球高手,你应该知道的事
- python中color语句_python中的colorlog库详解