前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >jquery调WCF

jquery调WCF

作者头像
liulun
发布2022-05-09 10:53:31
3190
发布2022-05-09 10:53:31
举报
文章被收录于专栏:liulunliulun

在项目中用过一些WCF的技术 这篇文章是对以前用过的一点东西的一个梳理

一,webconfig的配置 除了一般的配置外,与WCF相关的配置如下

代码语言:javascript
复制
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AllenBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service name="jqueryWCF.WCFservice">
        <endpoint address="" behaviorConfiguration="AllenBehavior"  binding="webHttpBinding" contract="jqueryWCF.WCFservice" />
      </service>
    </services>
  </system.serviceModel>

其中<service>节点中的name属性,是实现了服务契约的类型名,类型名必须是完整的,要包括名称空间 <endpoint>节点的address属性为空,说明使用基地址. behaviorConfiguration属性与behavior节点的name属性相匹配 binding属性说明WCF服务使用什么协议,这里是HTTP协议 contract属性是描述契约的接口名称,也必须是完整的.如果没有接口直接写实现契约的类型名也可以(我这里就是这样).

<behavior>节点的信息是描述WCF服务端的一些特性,行为的

<behavior name="AllenBehavior"> name属性与前面说的behaviorConfiguration属性一致 <enableWebScript />节点使我们的WCF支持ajax <serviceHostingEnvironment aspNetCompatibilityEnabled="true" /> 与后端的AspNetCompatibilityRequirements配合使用

二:页面中的js代码 这段JS是写在JQUERY框架下面的

代码语言:javascript
复制
function callServer(){
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());
    $.ajax({
        type: 'post',
        url: '/WCFservice.svc/InsertRow',
        contentType: 'text/json',
        data: '{"id":'+id+',"title":"'+title+'","content":"'+content+'"}',
        success: function(msg) {
        var a = eval('('+msg+')');
        if(String(a.d).length>0){alert(a.d);}
        else{alert("服务器超时");}
        }
    });
    }

其中 $.ajax(.....)是框架提供的一个调用ajax的方法,兼容目前大多数浏览器

url: '/WCFservice.svc/InsertRow' 这里是WCF的地址+方法名

contentType: 'text/json',

这是以JSON的方式POST数据,当然也可以用XML的方式(要配合WCF后端的定义)

data: '{"id":'+id+',"title":"'+title+'","content":"'+content+'"}',

数据必须按照InsertRow方法的签名传递(这里稍有不慎就出错了,而且js的调试比较难搞)

success: function(msg) {}

成功后的回调函数,msg参数是一个object类型的,要eval()一下才能得到里面的数据

三:后端WCF代码

代码语言:javascript
复制
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace jqueryWCF
{
    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class WCFservice
    {
        [OperationContract]
        [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        public string InsertRow(int id,string title,string content)
        {
            return string.Format("您输入的标题是:{0}\n\n您输入的内容是:{1}\n\n此文章的id是:{2}",title,content,id.ToString());
        }
    }
}

系统要引用System.ServiceModel.Web的DLL默认是不引用的

ServiceContract属性把此类型公开在WCF服务中

AspNetCompatibilityRequirements属性确保端点使用了WEBHTTP绑定模型 与webconfig中的<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />配合使用 OperationContract属性把方法公开在WCF服务中 RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json 说明传递近来的数据都是JSON形式的,只有两种形式,一个是JSON,一个是XML. (我觉得JSON更"对象"一点,XML更"数据"一点)

BodyStyle = WebMessageBodyStyle.WrappedRequest

是把参数包装一下 这样可以传递多个参数进来,

我猜返回值之所以是一个javascript的对象(json流得来的对象)也是因为这里 项目示例:点此下载

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-03-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档