C#WCF Web API + JSONP
-
25-10-2019 - |
题
有没有简单的方法使JSONP为新的WCF Web API REST服务工作?
我尝试过没有运气
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name=""
helpEnabled="true"
automaticFormatSelectionEnabled="true"
defaultOutgoingResponseFormat ="Json"
crossDomainScriptAccessEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
解决方案
更新:具有集成JSONP支持的最新WCF Web API位船,而用法几乎与上面链接中描述的方式相似。
其他提示
您可以检查 以下博客文章 用于在.NET 4.0中与WCF一起使用JSONP。
只是想提供有关JSONP开箱即用的WebAPI的更多详细信息。我很难找到这些信息,所以也许会帮助别人...
这个线程 在WCF Codeplex上,有Daniel Roth的描述,讲述了如何使用JQUERY使用WebAPI跨域JSON查询(又名JSONP)。
他参考的“示例”可以在WCF Codeplex存储库中找到 这里. 。它在“默认”文件夹中。
另外,请确保使用Nuget安装WebApienhancements预览6,否则这些都无法使用。
您需要一个具有以下内容的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);
});
这是 另一个博客文章 描述了如何添加 JsonpFormatter
到一个项目。
不隶属于 StackOverflow