2012-04-10 3 views
7

Ich rufe (Ajax Request) einen WCF REST Service an und die Anfrage ist eine Cross Domain Anfrage.Übergreifende Domain jQuery Ajax Anfrage & WCF REST Service

Wenn ich meinen Dienst in der gleichen Domäne bereitstellen, funktioniert alles wie Sahne. Schließlich wird der Dienst in der Produktion in einer anderen Domäne sein.

Ich benutze jQuery 1.5.2. Mein Dienst gibt mir einen Fehler Spruch:

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

Obwohl in Firefox ich den JSON-Wert sehen kann, aber die Ausführung fällt auf die Fehlerbehandlung von Ajax-Anfrage.

Meine Ajax-Anfrage ist:

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); 
     } 
    }); 
} 

auf WCF-Dienst Seite, ich habe konfiguriert CrossDomainScriptAccess auf true:

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

JSON-Antwort, die ich von dem Server erhalten wird:

[{"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)\/"}] 

Fehle ich hier etwas in den Einstellungen. Der gesamte Code funktioniert einwandfrei, wenn der Dienst in dieselbe Domäne verschoben wird.

Ich schaute in ähnliche Post, konnte aber nicht funktionieren.

+0

Ich hoffe, dass Sie auch Cross-Domain-Richtliniendatei hinzugefügt haben, hier überprüfen http://msdn.microsoft.com/en-us/library/cc197955%28v = vs.95% 29.aspx – Chandermani

+0

Ja, das ist schon in der Wurzel –

Antwort

5

Nun, ich habe es selbst herausgefunden. Lösung war die Konfigurationsdatei hält die Servicedetails

Ich habe Standard-Endpoint und die Bindung in der Konfigurationsdatei

<standardEndpoints> 
     <webScriptEndpoint> 
     <standardEndpoint crossDomainScriptAccessEnabled="true"> 
     </standardEndpoint> 
     </webScriptEndpoint> 
     </standardEndpoints> 



    <bindings> 

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

zu welchem ​​Abschnitt Ihrer web.config haben Sie das hinzugefügt? –

+0

@ matthew_360 unter dem Tag , das sich unter dem übergeordneten -Tag befindet –

+0

Wie können wir auch bestimmte Domänen in der Konfiguration angeben, um den "domainübergreifenden" Zugriff zu ermöglichen? – Virus

2

ich auch <webHttpEndpoint> hinzufügen erforderlich zu modifizieren, um es zu bekommen arbeiten:

<standardEndpoints> 
    <webHttpEndpoint> 
     <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint> 
    </webHttpEndpoint> 
    <webScriptEndpoint> 
     <standardEndpoint crossDomainScriptAccessEnabled="true"></standardEndpoint> 
    </webScriptEndpoint> 
</standardEndpoints> 

<bindings> 
    <webHttpBinding> 
     <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> 
    </webHttpBinding> 
</bindings> 
+0

Ich schätze Ihre sauberere Formatierung –

0
[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-Aufruf zu Wcf-Dienst

$.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); 

       } 
      } 
     });