惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
T
The Blog of Author Tim Ferriss
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
量子位
I
InfoQ
Jina AI
Jina AI
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
J
Java Code Geeks
Recent Announcements
Recent Announcements
aimingoo的专栏
aimingoo的专栏
小众软件
小众软件
V
V2EX
腾讯CDC
P
Proofpoint News Feed
A
About on SuperTechFans
爱范儿
爱范儿
U
Unit 42
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Last Week in AI
Last Week in AI

博客园 - 空紫竹

Crystal Report Error: Either the Crystal Reports registy key permission are insufficient or the Crystal Reports runtime is not installed correctly SSRS 例子的网站 SSRS配置 获取数据库中有默认值的所以字段 sql server xml nodes 的使用 Could not load type 'System.ServiceModel.Activation.HttpModule' from assembly 'System.ServiceModel, Version=3.0.0.0 IIS 时间问题 sql server 创建临时表 系统程序监控软件 Window 7sp1 安装vs2010 sp1 打开xaml文件崩溃 windows 2008 安装 sql server 2008 详解Silverlight中PivotViewer控件编程实例 CSS资源网址 SQL server 2008 安装和远程访问的问题 vss File for <file> (<physical file>) Was Not Found silverlight 报错超时 sql server isnumeric 函数 asp 中的getChunk(img_size) Windows 7 恢复删除的数据
JQuery, Silverlight 公用WCF
空紫竹 · 2013-07-26 · via 博客园 - 空紫竹

WCF web.config配置:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="" closeTimeout="00:30:00" openTimeout="00:30:00"
          receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferSize="2147483647"
          maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647"
            maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <!--below config is for https-->
          <!--<security mode="Transport">
            <transport clientCredentialType="None" />
          </security>-->
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service name="WcfHost.Service2">
        <endpoint address="Ajax" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" contract="WcfHost.Service2" />
        <endpoint address="Siverlight"
          binding="basicHttpBinding" contract="WcfHost.Service2" />        
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

在JQuery使用:

<script type="text/javascript">
  var Type;
  var Url;
  var Data;
  var ContentType;
  var DataType;
  var ProcessData;

  function WCFJSON() {
    Type = "POST";
    Url = "/WcfHost/Service2.svc/Ajax/GetTest";
    ContentType = "application/json; charset=utf-8";
    DataType = "json";
    ProcessData = true;
    CallService();
  }

  function CallService() {
    $.ajax({
      type: Type, //GET or POST or PUT or DELETE verb
      url: Url, 
      data: Data, 
      contentType: ContentType,
      dataType: DataType, 
      processdata: ProcessData,
      success: function (msg) {
        ServiceSucceeded(msg);
      },
      error: ServiceFailed
    });
  }

  function ServiceFailed(xhr) {
    alert('Service call failed: ' + xhr.status + '' + xhr.statusText);
    Type = null;
    varUrl = null;
    Data = null;
    ContentType = null;
    DataType = null;
    ProcessData = null;
  }

  function ServiceSucceeded(result) {
    if (DataType == "json") {
      alert(result.d);
    }
  }

  $(document).ready(
    function () {
      WCFJSON();
    }
);
</script>

WCF文件

namespace WcfHost
{
  [ServiceContract(Namespace = "")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class Service2
  {
    [OperationContract]
    public void DoWork()
    {
      // Add your operation implementation here
      return;
    }
    // Add more operations here and mark them with [OperationContract]
    [OperationContract]
    public string GetTest()
    {
      return "Test";
    }
  }
}