• jquery调WCF


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

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

    <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框架下面的
    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代码
    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流得来的对象)也是因为这里



    项目示例:点此下载
  • 相关阅读:
    (二十五)Struts2 Tiles集成
    (二十四)Struts2 Spring集成
    etcd 和 redis的使用场景
    react v16.12 源码阅读环境搭建
    gmail邮箱怎么批量删除邮件
    动态创建的元素怎么做动画
    Window 添加定时任务
    commons-pool2-中的一些配置
    Activiti 5.18 流程Model 转成 流程BPMN文件
    Activiti 使用自己的身份认证服务
  • 原文地址:https://www.cnblogs.com/liulun/p/1425382.html
Copyright © 2020-2023  润新知