2012-04-10 16 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リクエストがある:WCFサービス側で

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

、私がtrueにCrossDomainScriptAccessを設定している:

<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

+0

これはすでにweb.configのセクションに追加したルート –

答えて

5

私は自分で考え出しました。ソリューションは、サービスの詳細

を保持している設定ファイルを変更することでした私は、標準的なエンドポイントとを追加した、私はそれが仕事にも取得する <webHttpEndpoint>を追加するために必要な

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



    <bindings> 

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

にありますか? –

+0

@ matthew_360、親タグの下にあるタグの下に –

+0

"クロスドメイン"アクセスを許可するために、config内の特定のドメインを指定する方法を教えてください。 – Virus

2

設定ファイルにバインディング:

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

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

私はクリーナーのフォーマットに感謝します –

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> 

WCFサービスへのjQueryのAJAX呼び出し

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

       } 
      } 
     }); 
関連する問題