Cross domain jQuery Ajax Request & WCF REST Service

I call (Ajax request) the WCF REST service, and the request is a cross-domain request.

If I breed my service in the same domain, everything works like a cream. Ultimately, during production, the service will be in a different domain.

I am using jQuery 1.5.2. My service returns me an error message:

errorThrown: "jQuery15208493315000087023_1334089616458 was not called" textStatus: "parsererror" 

Although in Firefox I can see JSON values, but execution falls on the Ajax request error handler.

My Ajax request:

 function CallService() { $.ajax({ type: "GET", url: "http://SomeService/EmpService.svc/GetValues?dv=1455", contentType: "application/json; charset=utf-8", dataType: "jsonp", processdata: false, success: function (data) { ServiceSucceeded(data); }, error: function (jqXHR, textStatus, errorThrown) { debugger; alert("Service Error"); ServiceFailed(jqXHR, textStatus, errorThrown); } }); } 

On the WCF service side, I configured CrossDomainScriptAccess to true:

 <webHttpBinding> <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> </webHttpBinding> 

The JSON response I get from the server:

 [{"Message": "Stop On Duty", "MessageTime": "\/Date(1334068773893-0500)\/"}, {"Message": "Start On Duty", "MessageTime": "\/Date(1334068763540-0500)\/"}, {"Message": "App_testing_4102012924am", "MessageTime": "\/Date(1334068533627-0500)\/"}, {"Message": "Kunal_testing_4102012924am", "MessageTime": "\/Date(1334067945510-0500)\/"}, {"Message": "Alert: Door Open", "MessageTime": "\/Date(1334066280963-0500)\/"}] 

I don’t see anything here in the settings. All code works fine if the service is moved to the same domain.

I looked at a similar post, but could not do the job.

+7
source share
3 answers

It’s good that I understood myself. The solution was to modify the configuration file containing the service data

I added a standard endpoint and binding in the configuration file

 <standardEndpoints> <webScriptEndpoint> <standardEndpoint crossDomainScriptAccessEnabled="true"> </standardEndpoint> </webScriptEndpoint> </standardEndpoints> <bindings> <webHttpBinding> <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> </webHttpBinding> 
+5
source

I needed to add <webHttpEndpoint> to make it work:

 <standardEndpoints> <webHttpEndpoint> <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint> </webHttpEndpoint> <webScriptEndpoint> <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint> </webScriptEndpoint> </standardEndpoints> <bindings> <webHttpBinding> <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> </webHttpBinding> </bindings> 
+2
source
  [OperationContract] [WebGet( ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json, UriTemplate = "GetEmployeeJson")] List<EmployeeData> GetEmployeeJson(); 

Web.config

  <bindings> <webHttpBinding> <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> </webHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="WcfExample.Service1Behavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="WebBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> <services> <service behaviorConfiguration="WcfExample.Service1Behavior" name="WcfExample.Service1"> <endpoint address="" binding="webHttpBinding" contract="WcfExample.IService1" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="WebBehavior" /> </service> </services> 

Jquery ajax wcf service call

  $.ajax({ type: "GET", contentType: "application/javascript", crossDomain: true, dataType: 'jsonp', cache: true, url: 'http://localhost:49349/Service1.svc/GetEmployeeJson', success: function (data) { var html = []; alert(data[0].lastname); $.each(data, function (index, value) { $("#TableID").append("<tr><td>" + value.HREmpId + "</td><td>" + value.firstName + "</td><td>" + value.lastname + "</td><td>" + value.address + "</td><td>" + value.city + "</td></tr>"); }); }, error: function (xhr, ajaxOptions, thrownError) { alert("here error"); alert(thrownError); if (xhr != null) { var err = JSON.parse(xhr.responseText); //you can throw a code-behinde Exception and it will automatically //render to a valid JSON string when we rerieve the responseText alert("ErrorMessage: " + err.Message + " StackTrace: " + err.StackTrace); } } }); 
0
source

All Articles