【问题标题】:Cross Domain jQuery Ajax Request & WCF REST Service跨域 jQuery Ajax 请求和 WCF REST 服务
【发布时间】:2012-04-23 04:44:58
【问题描述】:

我正在调用(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)\/"}]

我是否在设置中遗漏了任何内容。如果将服务移动到同一个域,则整个代码都可以正常工作。

我查看了类似的帖子,但无法完成这项工作。

【问题讨论】:

标签: jquery wcf rest jsonp


【解决方案1】:

好吧,我自己想通了。解决方案是修改保存服务详细信息的配置文件

我在配置文件中添加了标准端点和绑定

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



  <bindings>

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

【讨论】:

  • 您将此添加到 web.config 的哪个部分?
  • @matthew_360 在父 标签下的 标签下
  • 我们如何在配置中指定特定域以允许“跨域”访问?
【解决方案2】:

我还需要添加 &lt;webHttpEndpoint&gt; 才能让它工作:

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

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

【讨论】:

    【解决方案3】:
     [OperationContract]
        [WebGet( ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare, RequestFormat=WebMessageFormat.Json,
        UriTemplate = "GetEmployeeJson")]
         List<EmployeeData> GetEmployeeJson();
    

    网页配置

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

    【讨论】:

      猜你喜欢
      • 2015-06-05
      • 2013-06-19
      • 2011-07-05
      • 1970-01-01
      • 2011-12-11
      • 2013-04-01
      • 2015-01-26
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多