Ria服务添加JSONP终端以启用跨域调用

Ria服务添加JSONP终端以启用跨域调用

本文介绍了如何为WCF Ria服务添加JSONP终端以启用跨域调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道WCF RIA Services具有一个Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory,可用于启用JSON.我需要通过JSONP启用跨域调用.是否有一个现成的DomainServiceEndpointFactory可以完成此任务?

I'm aware that WCF RIA Services has a Microsoft.ServiceModel.DomainServices.Hosting.JsonEndpointFactory that I can use to enable JSON.I need to enable cross-domain calls via JSONP. Is there an existing DomainServiceEndpointFactory that will accomplish this?

推荐答案

我只需要这样做-我覆盖了JsonEndpointFactory并在其中进行了绑定,然后使用新类添加了一个端点.

I just needed to do this - I overrode JsonEndpointFactory and tinkered with the binding in there, then added an endpoint using the new class.

namespace Bodge
{
    public class JsonPEndpointFactory : JsonEndpointFactory
    {
        public override IEnumerable<ServiceEndpoint> CreateEndpoints(DomainServiceDescription description, DomainServiceHost serviceHost)
        {
            IEnumerable<ServiceEndpoint> endPoints = base.CreateEndpoints(description, serviceHost);
            foreach (ServiceEndpoint endPoint in endPoints)
            {
                if (endPoint.Binding is WebHttpBinding)
                {
                    ((WebHttpBinding)endPoint.Binding).CrossDomainScriptAccessEnabled = true;
                }
            }

            return endPoints;
        }
    }
}

  <endpoints>
    <add name="JSONP" type="Bodge.JsonPEndpointFactory, Bodge, Version=1.0.0.0"/>
  </endpoints>

然后使用端点和回调查询参数访问您的服务,例如 http://blah/service.svc/JSONP/GetStuff?callback = callbackname

Then access your service with the endpoint and the callback query param e.g.http://blah/service.svc/JSONP/GetStuff?callback=callbackname

希望有帮助,克里斯.

Hope that helps,Chris.

这篇关于如何为WCF Ria服务添加JSONP终端以启用跨域调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 11:58