C# 如何为现有WCF服务本机启用JSONP?
我有一个类似以下方法的现有服务:C# 如何为现有WCF服务本机启用JSONP?,c#,asp.net,wcf,json,jsonp,C#,Asp.net,Wcf,Json,Jsonp,我有一个类似以下方法的现有服务: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)] public class SomeService : ISomething { public SomeListResults SomeLis
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class SomeService : ISomething
{
public SomeListResults SomeList(SomeParams someParams)
{
....
}
}
有没有一种简单的方法可以同时允许JSONP调用和JSON(检测它)。这是本机配置吗?将配置更新为:
提供创建跨域可访问的wcf服务的演练
这将使您的服务能够接受来自跨域源的请求
在决定是否添加您的响应(jsonp中的p)方面
感谢@carlosfigueira的帮助:
如果本机支持使用.NET4JSONP。只要请求有一个名为“callback”的查询字符串参数(可以配置此名称),响应就会用函数名填充
.
否则,您将需要编写一个定制的消息检查器来适当地填充响应。新的JSONP特性将通过WebHttpBinding公开。CustomerService的配置如下所示:
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="ServiceSite.CustomersService">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonP" contract="ServiceSite.CustomersService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
您不需要编写自定义检查器-本机支持.NET4.0JSONP。只要请求有一个名为“callback”的查询字符串参数(可以配置此名称),响应就会被函数名填充(假设crossDomainScriptAccessEnabled设置为true,如您所述)@carlosfigueira非常感谢您提供的这些宝贵信息,我最后一次尝试jsonp和WCF是在.NET3.5中。答案现已更新。顺便说一句,很棒的博客!
// Get the JsonP data
$.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers) {
alert('Received ' + customers.length + ' Customers');
});