当前位置: 首页 > news >正文

姜堰 做网站廊坊seo整站优化

姜堰 做网站,廊坊seo整站优化,音乐网站开发文档撰写模板,企业网站建设的基本原则不废话了,直奔主题吧 wcf端: 近几年比较流行restful,为了能让ajax调用,同时也为了支持restful风格的uri,在创建一个Ajax-enabled Wcf Service后,必须手动修改svc文件,指定Factory,即…

不废话了,直奔主题吧

wcf端:

近几年比较流行restful,为了能让ajax调用,同时也为了支持restful风格的uri,在创建一个Ajax-enabled Wcf Service后,必须手动修改svc文件,指定Factory,即:

<%@ ServiceHost Language="C#" Debug="true" Service="ajaxSample.HelloWorld" CodeBehind="HelloWorld.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

注:如果不添加Factory,则wcf将无法用类似http://localhost/helloWorld.svc/Hello/person/name 的restful方式直接访问。

同时还要去掉web.config中的<enableWebScript />即类似:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ajaxSample.HelloWorldAspNetAjaxBehavior">
          <!--<enableWebScript />-->
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="ajaxSample.HelloWorld">
        <endpoint address="" behaviorConfiguration="ajaxSample.HelloWorldAspNetAjaxBehavior"
          binding="webHttpBinding" contract="ajaxSample.HelloWorld" />
      </service>
    </services>
  </system.serviceModel>

好了,开始写代码,鉴于wcf调用时有GET/POST二种方式,下面把几种常用的情况都写一个示例方法:

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;namespace ajaxSample
{[ServiceContract(Namespace = "http://yjmyzz.cnblogs.com/")][AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]public class HelloWorld{/// <summary>/// 只能Post的Restful方法/// </summary>/// <param name="person"></param>/// <param name="welcome"></param>/// <returns></returns>[OperationContract][WebInvoke(Method = "POST", UriTemplate = "PostRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]public List<string> PostRestfulTest(string person,string welcome){List<string> result = new List<string>();result.Add("PostRestfulTest -> from server:");result.Add(person);result.Add(welcome);return result;}/// <summary>/// 只能Get的Restful方法/// </summary>/// <param name="person"></param>/// <param name="welcome"></param>/// <returns></returns>[OperationContract][WebInvoke(Method = "GET", UriTemplate = "GETRestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]public List<string> GETRestfulTest(string person, string welcome){List<string> result = new List<string>();result.Add("GETRestfulTest -> from server:");result.Add(person);result.Add(welcome);return result;}/// <summary>/// 即可Get与Post的Restful方法/// </summary>/// <param name="person"></param>/// <param name="welcome"></param>/// <returns></returns>[OperationContract][WebInvoke(Method = "*", UriTemplate = "RestfulTest/{person}/{welcome}", ResponseFormat = WebMessageFormat.Json)]public List<string> RestfulTest(string person, string welcome){List<string> result = new List<string>();result.Add("RestfulTest -> from server:");result.Add(person);result.Add(welcome);return result;}/// <summary>/// 只能Post的常规方法(注:Post方式,BodyStyle必须设置成WrappedRequest或Wrapped)/// </summary>/// <param name="person"></param>/// <param name="welcome"></param>/// <returns></returns>[OperationContract][WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]public List<string> PostTest(string person, string welcome){List<string> result = new List<string>();result.Add("PostRestfulTest -> from server:");result.Add(person);result.Add(welcome);return result;}/// <summary>/// 只能Get的常规方法/// </summary>/// <param name="person"></param>/// <param name="welcome"></param>/// <returns></returns>[OperationContract][WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]public List<string> GETTest(string person, string welcome){List<string> result = new List<string>();result.Add("GETTest -> from server:");result.Add(person);result.Add(welcome);return result;}}
}

  

jQuery调用代码:

    <script type="text/javascript">$().ready(function () {$.post("HelloWorld.svc/PostRestfulTest/111/222", function (data) {alert("PostRestfulTest调用成功,返回值为:" + data);})$.get("HelloWorld.svc/GETRestfulTest/333/444", function (data) {alert("GETRestfulTest调用成功,返回值为:" + data);})$.get("HelloWorld.svc/RestfulTest/555/666", function (data) {alert("RestfulTest GET方式调用成功,返回值为:" + data);})$.post("HelloWorld.svc/RestfulTest/777/888", function (data) {alert("RestfulTest POST方式调用成功,返回值为:" + data);})$.get("HelloWorld.svc/GETTest", { person: "aaa", welcome: "bbb" }, function (data) {alert("GETTest 调用成功,返回值为:" + data);});$.ajax({url: "HelloWorld.svc/PostTest",type: "POST",contentType: "application/json",data: '{"person":"ccc","welcome":"ddd"}',dataType: "html",success: function (data) { alert("PostTest调用成功,返回值为:" + data); }});})</script>

有时候,WCF暴露的方法中可能需要一些敏感信息做为参数(比如用户名/用户ID之类),这时如果直接用js来调用wcf,可能会把这部分信息泄漏在客户端,这种场景下,我们也经常用一个服务端的ashx来做中转

TestService.svc

using System.ServiceModel;namespace ashx_jQuery
{[ServiceContract]public class TestService {/// <summary>/// 获取当前用户指定月份的工资/// </summary>/// <param name="userId"></param>/// <param name="month"></param>/// <returns></returns>[OperationContract]public double GetSalary(int userId,int month){if (month == 1)//只是演示而已{return 5000;}else {return 1000;}}}
}

 AjaxProcess.ashx

using System.Web;namespace ashx_jQuery
{/// <summary>/// Summary description for AjaxProcess/// </summary>public class AjaxProcess : IHttpHandler{public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";string month = context.Request["month"];TestService wcf = new TestService();double salary = wcf.GetSalary(GetUserId(), int.Parse(month));context.Response.Write("{salary:" + salary + "}");}/// <summary>/// 获取当前的用户ID/// </summary>/// <returns></returns>private int GetUserId() {return 1;}public bool IsReusable{get{return false;}}}
}

 jQuery调用:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="ashx_jQuery._default" %><!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>jQuery ashx Sample</title><script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js"></script><script type="text/javascript">$().ready(function () {$("#btnTest").click(function () {$.post("AjaxProcess.ashx",{ month:1 },function (e) {var d = eval("(" + e + ")");alert(d.salary);}, "html");})})</script>
</head>
<body><form id="form1" runat="server"><input type="button" value="GetSalary" id="btnTest"/></form>
</body>
</html>

 示例代码:

http://files.cnblogs.com/yjmyzz/jquery_ajax_wcf_rest.zip

转载于:https://www.cnblogs.com/yjmyzz/archive/2011/10/11/2207923.html

http://www.lbrq.cn/news/2496781.html

相关文章:

  • 自己家的电脑宽带50m做网站服务器百度一下你就知道百度官网
  • 更换网站后台管理系统神起网络游戏推广平台
  • 南昌正规网站公司5g网络优化培训
  • 那个网站可以做图标一个产品的宣传和推广方案
  • 高端 网站开发交换友情链接的途径有哪些
  • 企业网站建设怎么选择空间聚合搜索引擎入口
  • 腾讯云如何建设网站首页福州seo招聘
  • 可以看所有网站的浏览器网站seo策划
  • 机械手表网站百度推广一级代理商名单
  • 温州网站定制哪家好属于b2b的网站有哪些
  • 做cp和网站运营付费推广外包
  • wordpress表情外贸网站优化公司
  • 常州市网站建设深圳龙岗区布吉街道
  • 郑州建立网站百度大数据
  • 网站收录怎么提高快速排名优化系统
  • 河南便宜网站建设价格兰州seo新站优化招商
  • 网站架构图的制作网络营销到底是个啥
  • 企业网站建站元素googleplay安卓版下载
  • 厂字型布局网站例子中国搜索引擎有哪些
  • 在网站做登记表备案 如果修改优化大师破解版app
  • wdcp拒绝访问网站十大免费无代码开发软件
  • wordpress次级目录ftp廊坊seo关键词优化
  • 好看的学校网站模板免费下载关键词歌词含义
  • 徐州建站软件现在有什么推广平台
  • 网站服务器试用百度的总部在哪里
  • 怎么仿别人的网站近几天发生的新闻大事
  • 网站建设教程答允苏州久远网络产品推广宣传方案
  • 广州企业网站建设推荐网店营销策略有哪些
  • 网站建设咨询公国内新闻最新消息今天
  • 珠海市网站建设公司网站建设与优化
  • C++模板进阶:从基础到实战的深度探索
  • dapp前端⾯试题
  • Spring AI 1.0 提供简单的 AI 系统和服务
  • MySQL 8.4 Windows 版安装记录与步骤参考
  • 【element-ui】HTML引入本地文件出现font找不到/fonts/element-icons.woff
  • B站直播视频 | 深度讲解 Yocto 项目:从历史、架构到实战与趋势