2012-04-10 14 views
7

Dzwonię (żądanie Ajax) usługi REST WCF, a żądanie jest żądaniem między domenami.Cross Domain jQuery Zapytanie Ajax i usługa REST WCF

Jeśli wdrażam moją usługę w tej samej domenie, wszystko działa jak krem. Ostatecznie w produkcji usługa będzie znajdować się w innej domenie.

Używam jQuery 1.5.2. Moje usługi zwraca mi powiedzenie o błędzie:

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

Chociaż Firefox widzę wartości JSON, ale wykonanie spada do obsługi błędów żądania Ajax.

Moja prośba Ajax jest:

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

Na stronie WCF Service mam skonfigurowane CrossDomainScriptAccess true:

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

JSON reakcja które otrzymuję z serwera jest:

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

Czy brakuje mi tu niczego w ustawieniach. Cały kod działa poprawnie, jeśli usługa zostanie przeniesiona do tej samej domeny.

Zajrzałem do podobnego stanowiska, ale nie mogłem tego wykonać.

+0

Mam nadzieję, że również dodać przekrój plik zasad domeny, sprawdź tutaj http://msdn.microsoft.com/en-us/library/cc197955%28v = vs.95% 29.aspx – Chandermani

+0

tak, to już jest w katalogu głównym –

Odpowiedz

5

Cóż, wymyśliłem na własną rękę. Rozwiązaniem było zmodyfikować plik konfiguracyjny trzymając informacje dotyczące usług

dodałem standardowa punktów końcowych i wiązanie w pliku konfiguracyjnym

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



    <bindings> 

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

do której sekcji twojego web.config dodałeś to? –

+0

@ matthew_360 w tagu , który znajduje się pod nadrzędnym znacznikiem

+0

W jaki sposób możemy również określić określone domeny w konfiguracji, aby zezwolić na dostęp do "Cross Domain"? – Virus

2

Musiałem też dodać <webHttpEndpoint> dostać to działa:

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

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

Doceniam czystsze formatowanie –

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 wywołanie usługi WCF

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

       } 
      } 
     });