【问题标题】:C# WCF Web API + JSONPC# WCF Web API + JSONP
【发布时间】:2011-10-11 19:24:14
【问题描述】:

有没有一种简单的方法可以让 JSONP 为新的 WCF Web API 休息服务工作?

我试过这个没有运气

<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name=""
                      helpEnabled="true"
                      automaticFormatSelectionEnabled="true"
                      defaultOutgoingResponseFormat ="Json"
                      crossDomainScriptAccessEnabled="true"/>
  </webHttpEndpoint>
</standardEndpoints>

【问题讨论】:

    标签: c# c#-4.0 wcf-web-api


    【解决方案1】:

    https://alexanderzeitler.com/articles/Look-Ma,-I-can-handle-JSONP-%28aka-Cross-Domain-JSON%29-with-WCF-Web-API-and-jQuery!/

    更新: 最新的 WCF Web API 位附带集成的 JSONP 支持,而用法几乎与上面链接中描述的方式相似。

    【讨论】:

    【解决方案2】:

    只是想提供更多关于 WCF WebAPI 对 JSONP 的开箱即用支持的详细信息。我很难找到这些信息,所以也许它会帮助其他人......

    This thread 在 WCF CodePlex 上有 Daniel Roth 关于如何使用 jQuery 使用 WebApi 跨域 JSON 查询(​​又名 JSONP)的描述。

    他引用的“示例”可以在 WCF CodePlex 存储库here 中找到。它位于“默认”文件夹中。

    另外,请确保您使用 NuGet 为 Preview 6 安装 WebApiEnhancements,否则这些都不起作用。

    你需要一个 Global.asax.cs 类似于以下内容...

    public class Global : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            var config = new WebApiConfiguration() { EnableTestClient = true };
            RouteTable.Routes.MapServiceRoute<HelloWorldApi>("api", config);
        }
    }
    

    另一个关键是在您的 URI 模板中考虑“扩展”...

    [WebGet(UriTemplate="hello{ext}")]
    

    然后你像这样进行 jQuery 调用...

    $.getJSON("/api/hello.jsonp?callback=?", function (data) {
        $("div").html(data);
    }); 
    

    【讨论】:

      【解决方案3】:

      这里的another blog post 描述了如何将JsonpFormatter 添加到项目中。

      【讨论】:

        【解决方案4】:

        您可以查看following blog post 在 .NET 4.0 中将 JSONP 与 WCF 结合使用。

        【讨论】:

          猜你喜欢
          • 2013-11-12
          • 2023-03-16
          • 2012-03-14
          • 1970-01-01
          • 2015-02-21
          • 2013-06-24
          • 2011-10-08
          • 2014-08-20
          • 2011-09-13
          相关资源
          最近更新 更多