2012-04-10 86 views
7

我正在调用(Ajax请求)一个WCF REST服务,并且请求是跨域请求。跨域jQuery Ajax请求和WCF REST服务

如果我在同一个域中部署我的服务,那么所有的工作都像奶油一样。最终在生产中,该服务将处于不同的领域。

我正在使用jQuery 1.5.2。我的服务返回我一个错误说:

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

虽然在Firefox中,我可以看到的JSON值,但执行降到Ajax请求的错误处理程序。

我的Ajax请求是:

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

在WCF服务的一面,我已经配置CrossDomainScriptAccess为true:

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

JSON响应这是我从服务器得到的是:

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

我在设置中遗漏了什么。如果服务移动到同一个域,整个代码都可以正常工作。

我看着类似的帖子,但无法做到这一点。

+0

,我希望你也加入了跨域策略文件,点击这里http://msdn.microsoft.com/en-us/library/cc197955%28v = vs.95%29.aspx – Chandermani 2012-04-11 06:40:20

+0

是的,根目录 – 2012-04-11 13:36:00

回答

5

好吧,我想出了我自己的。解决办法是修改配置文件存储业务细节

我添加标准端点,并在配置文件中

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



    <bindings> 

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

已经存在,你的web.config中的哪一部分是你添加的? – 2013-01-18 22:38:23

+0

@ matthew_360位于标签下,位于父标签下 – 2016-01-15 13:36:59

+0

我们如何在配置中指定特定域以允许“跨域”访问? – Virus 2017-03-22 11:17:12

2

我需要还可以添加<webHttpEndpoint>得到它的工作的结合:

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

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

我很欣赏你更清晰的格式 – 2016-10-11 19:28:49

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调用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); 

       } 
      } 
     });