How to enable JSONP for an existing WCF service?

I have an existing service, such as the method below:

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

Is there an easy way to resolve JSONP calls as well as JSON at the same time (detect it). Is it native?

+6
source share
2 answers

Update your configuration to look like this:

 <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> 

See here to post a blog post , giving a step-by-step guide on creating a wcf service available for a cross-domain.

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

In terms of determining whether to defer your answer (p in jsonp),

Thanks @carlosfigueira for this:

When using .Net 4 JSONP is supported natively. As long as the request has a query string parameter called a callback (this name can be configured), the response will be supplemented by the name of the function,

Otherwise, you will need to write a special message inspector that will correctly answer the answer.

+9
source

A new JSONP function is opened via WebHttpBinding. The configuration for CustomerService will look like this:

  <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> 

Using JSONP with jQuery

  // Get the JsonP data $.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers) { alert('Received ' + customers.length + ' Customers'); }); 
+2
source

Source: https://habr.com/ru/post/902067/


All Articles