如何为现有的 WCF 服务本地启用 JSONP? [英] How to natively enable JSONP for existing WCF service?

查看:27
本文介绍了如何为现有的 WCF 服务本地启用 JSONP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的服务,如下面的方法:

I have a an existing service like the below method:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class SomeService : ISomething
{
    public SomeListResults SomeList(SomeParams someParams)
    {
          ....
    }
}

是否有一种简单的方法可以同时允许 JSONP 调用和 JSON(检测它).这是原生的吗?

Is there a simple way to allow JSONP calls and also JSON at the same time (detect it). Is this native?

推荐答案

将您的配置更新为:

<configuration>
  <system.web>
    <compilation debug="true" targetframework="4.0">
    <authentication mode="None">
  </authentication></compilation></system.web>
  <system.webserver>
    <modules runallmanagedmodulesforallrequests="true">
  </modules></system.webserver>
  <system.servicemodel>
    <servicehostingenvironment **aspnetcompatibilityenabled**="true">
    <standardendpoints>
      <webscriptendpoint>
        <standardendpoint **crossdomainscriptaccessenabled**="true" name="">
      </standardendpoint></webscriptendpoint>
    </standardendpoints>
  </servicehostingenvironment></system.servicemodel>
</configuration>

请参阅此处获取博文 提供创建可跨域访问的 wcf 服务的演练.

See here for a blog post providing a walkthrough of creating a wcf service that's accessible cross-domain.

这将使您的服务能够接受来自跨域来源的请求.

This will enable your service to accept requests from cross-domain sources.

在确定是否填充响应(jsonp 中的 p)方面,

In terms of determining whether to pad your response (the p in jsonp),

感谢@carlosfigueira:

Thanks to @carlosfigueira for this:

如果使用 .Net 4 JSONP 是本机支持的.只要请求有一个名为callback"的查询字符串参数(这个名字可以配置),响应就会填充函数名.

If using .Net 4 JSONP is supported natively. As long as the request has a query string parameter called "callback" (this name can be configured), the response will be padded with the function name .

否则,您需要编写一个自定义消息检查器来适当地填充响应.

Otherwise, you'll need to write a custom message inspector that pads the response appropriately.

这篇关于如何为现有的 WCF 服务本地启用 JSONP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆