如何为现有的WCF服务本机启用JSONP?
我有一个像以下方法的现有服务:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class SomeService : ISomething { public SomeListResults SomeList(SomeParams someParams) { .... } }
是否有一种简单的方法可以同时允许JSONP调用和JSON(检测它)。 这是原生的吗?
将配置更新为:
请参阅此处获取博客文章,其中提供了创建可访问跨域的wcf服务的演练。
这将使您的服务能够接受来自跨域源的请求。
在确定是否填充您的响应(jsonp中的p)方面,
感谢@carlosfigueira:
如果使用.Net 4本机支持JSONP。 只要请求具有名为“callback”的查询字符串参数(可以配置此名称),响应将使用函数名称填充。
否则,您需要编写一个自定义消息检查器,以适当地填充响应。
新的JSONPfunction通过WebHttpBinding公开。 CustomersService的配置如下所示:
用jQuery消耗JSONP
// Get the JsonP data $.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers) { alert('Received ' + customers.length + ' Customers'); });