2012-04-10 40 views
7

Tôi đang gọi (Yêu cầu Ajax) một dịch vụ WCF REST và yêu cầu là một yêu cầu miền chéo.Tên miền chéo Yêu cầu jQuery Ajax & Dịch vụ REST WCF

Nếu tôi triển khai dịch vụ của mình trong cùng một miền, mọi thứ hoạt động như kem. Cuối cùng trong sản xuất, dịch vụ sẽ nằm trong một miền khác.

Tôi đang sử dụng jQuery 1.5.2. Dịch vụ của tôi trả về cho tôi một lỗi nói rằng:

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

Mặc dù trong Firefox, tôi có thể thấy giá trị JSON nhưng thực thi rơi vào trình xử lý lỗi của yêu cầu Ajax.

yêu cầu Ajax của tôi là:

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 WCF Service bên, tôi đã cấu hình CrossDomainScriptAccess true:

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

JSON phản ứng mà tôi nhận được từ các máy chủ là:

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

Tôi có thiếu gì ở đây trong cài đặt không. Toàn bộ mã hoạt động tốt nếu dịch vụ được chuyển đến cùng một tên miền.

Tôi đã xem bài đăng tương tự nhưng không thể thực hiện tác phẩm này.

+0

Tôi hy vọng bạn cũng đã thêm vào tập tin chính sách tên miền chéo, kiểm tra ở đây http://msdn.microsoft.com/en-us/library/cc197955%28v = vs.95% 29.aspx – Chandermani

+0

có đã có sẵn trong thư mục gốc –

Trả lời

5

Tôi đã tự mình tìm ra. Giải pháp là thay đổi tập tin cấu hình giữ chi tiết dịch vụ

tôi đã thêm tiêu chuẩn Endpoint và các ràng buộc trong file config

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



    <bindings> 

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

mà bạn đã thêm phần web.config vào phần nào? –

+0

@ matthew_360 trong thẻ nằm trong thẻ cha mẹ –

+0

Làm cách nào chúng tôi cũng có thể chỉ định các miền cụ thể trong cấu hình để cho phép truy cập "Miền chéo"? – Virus

2

tôi cần cũng để thêm <webHttpEndpoint> để làm cho nó làm việc:

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

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

Tôi đánh giá cao việc định dạng sạch hơn của bạn –

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 gọi đến dịch vụ 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); 

       } 
      } 
     }); 
Các vấn đề liên quan