zoukankan      html  css  js  c++  java
  • jquery 调用wcf project

    最近项目中涉及到比较多的jquery调用后台的方法,开始的做法大都是新建ashx 文件,项目后期导致项目中新建了很多的ashx文件,当然也可以用webservice,或者是在web 项目中建wcf 文件,为了以后统一管理方便,打算新建wcf project ,把所有的文件放在一个统一项目中,部署一台服务器来提供统一的服务。

       后来在使用过程中发现了几个问题,具体的可以参考 dudu 的文章 

      把我项目中碰到的问题记录下来。 

      就用Wcf 项目 自带的例子,我就修改了文件的名称

      接口如下:

        [ServiceContract(Name = "CmarketService", SessionMode = SessionMode.NotAllowed, ProtectionLevel = ProtectionLevel.None)]

        public interface ICmarketService

        {

            [OperationContract]

            [WebGet]

            //[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

            //[WebInvoke(ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]

            string GetData(int value); 

        } 


    Service 端:RequirementsMode 必须为AspNetCompatibilityRequirementsMode.Allowed 或者是AspNetCompatibilityRequirementsMode.Required,不然运行的时候会提示The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.

       [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] 

        //[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

        public class CmarketService : ICmarketService

        { 

         public string GetData(int value)

            {

                return string.Format("You entered:{0}", value);

            }

    }

    wcf 项目的配置文件

    <configuration>

      <system.web>

        <compilation debug="true" targetFramework="4.0" />

      </system.web>

     <system.serviceModel>

        <diagnostics wmiProviderEnabled="true">

          <messageLogging logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true" />

        </diagnostics>

        <services>

           <service name="CMSService.CmarketService">

              <endpoint address="" behaviorConfiguration="ServiceBehavior"

                 binding="webHttpBinding" name="CmarketService" contract="CMSService.ICmarketService" />

              <endpoint address="mex" binding="mexHttpBinding" name="Mex" contract="IMetadataExchange" />

              <host>

                 <baseAddresses>

                    <add baseAddress="http://localhost/CmarketService/" />

                 </baseAddresses>

              </host>

           </service>

        </services>

        <behaviors>

          <endpointBehaviors>

            <behavior name="ServiceBehavior">

              <enableWebScript/>

            </behavior>

          </endpointBehaviors>

          <serviceBehaviors>

            <behavior>

              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

              <serviceMetadata httpGetEnabled="true" />

              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->

              <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />

            </behavior>

          </serviceBehaviors>

        </behaviors>

        <serviceHostingEnvironment  aspNetCompatibilityEnabled="true">

        </serviceHostingEnvironment>

      </system.serviceModel> 

     <system.webServer>

        <modules runAllManagedModulesForAllRequests="true"/>

      </system.webServer>

    </configuration>

    下来就可以在项目中使用了,调用方法如下

    $(document).ready(function () {

                $.getJSON('http://localhost/CmarketService/CmarketService.svc/GetData',

                    function (data) {

                        debugger;

                        alert(data.name)

                    });

                $.ajax({

                    url: 'http://localhost/CmarketService/CmarketService.svc/GetData',

                    data: {"value":"5"},

                    type: 'get',

                    //dataType: 'json',

                    contentType: 'text/json',

                    success: function (data) {

                        debugger;

                        if (data.d) {

                        }

                    },

                    error: function (xhr) {

                        //debugger;

                        //alert(xhr.responseText);

                    }

                });  

            }); 

     我开始的时候参考了dudu 的做法,在service 的方法加上WebInvoke 标签,后来发现调用的时候提示400 bad request,后来修改成 Webget ,调用成功。

    Download Code
  • 相关阅读:
    String.getBytes()未设置字符集导致打印的pdf乱码
    git更新代码报错,error: The following untracked working tree files would be overwritten by ch
    thinkpad X1 extreme 安装Ubuntu 18.04.2 LTS
    plsql的sql窗口中文模糊查询没有作用
    mysql 触发器和存储过程组合使用,实现定时触发操作
    css 实现table 隔行变色
    meta标签详解:源http://blog.csdn.net/kongjiea/article/details/17092413
    Spring+Quartz实现定时任务的配置方法
    ECToch随笔
    转载:ecshop自定义销量
  • 原文地址:https://www.cnblogs.com/jfliuyun/p/1829099.html
Copyright © 2011-2022 走看看