2016-05-05 5 views
1

初めてのWCFサービスを作成しました。他のクライアントと共有したいと思います。私はAccess-Control-Allow-OriginヘッダーをIIS、Web.config、およびGlobal.asaxに追加しましたが、リモートクライアントは引き続きこのサービスにアクセスできません。私はChromeとIE(私たちの組織のサポートされているブラウザ)でテストしました。私のエラーは "プリフライト要求への応答はアクセス制御チェックを通過しません:いいえ 'Access-Control-Allow-Origin'ヘッダーが要求されたリソースに存在します。 Web.configファイルでAccess Control-Allow-OriginはIISにありますが、動作しません。

:Global.asaxの中

<httpProtocol> 
    <customHeaders> 
    <add name="Access-Control-Allow-Origin" value="*" /> 
    <add name="Access-Control-Allow-Headers" value="Content-Type"/> 
    <add name="Access-Control-Allow-Methods" value="POST,GET,OPTIONS"/> 
    </customHeaders> 
</httpProtocol> 

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
    { 
     //These headers are handling the "pre-flight" OPTIONS call sent by the browser 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
     HttpContext.Current.Response.End(); 
    } 
} 

そして、ここで彼らはIISにあります。ここではScreen shot of IIS

JavaScriptがアクセスしようとしていますサービス。

$.ajax({ 
    type: "POST", 
    url: "http://localhost/wsSamwise/Service1.svc/GetTime", 
    data: '{"UserName": "' + userName + '"}', 
    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function(data) { 
     $("#results").html(data.GetTimeResult); 
     console.log(data); 
    } 
}); 

これらのアイデアのすべては、様々なstackoverflowのスレッドから来たが、それらのどれも私のために働いていません:それはローカルではなくリモートで動作します。私は何を逃したのですか?

EDIT:ローカルホストとリモートブラウザの応答のヘッダーをチェックアウトしました。自分のlocalhostの応答にはAccess-Control-Allow-Originヘッダーが表示されますが、リモートブラウザーの応答には表示されません。それは送られていないようです。

私はlocalhostからの応答ヘッダー:

Access-Control-Allow-Headers:Content-Type 
Access-Control-Allow-Methods:POST,GET,OPTIONS 
Access-Control-Allow-Origin:* 
Access-Control-Allow-Origin:* 
Cache-Control:private 
Content-Length:82 
Content-Type:application/json; charset=utf-8 
Date:Thu, 05 May 2016 16:01:28 GMT 
Server:Microsoft-IIS/7.5 
X-AspNet-Version:4.0.30319 
X-Powered-By:ASP.NET 

リモートブラウザからの応答:

Allow:OPTIONS, TRACE, GET, HEAD, POST 
Content-Length:0 
Date:Thu, 05 May 2016 16:00:09 GMT 
Public:OPTIONS, TRACE, GET, HEAD, POST 
Server:Microsoft-IIS/7.5 
X-Powered-By:ASP.NET 

EDIT 2:Guysは、多分それは完全にIISの問題です。私はちょうどIISにカスタムヘッダーを追加しました。ローカルホストブラウザに表示されますが、クライアント上には表示されません。リモートブラウザからBody-Countヘッダーが欠落していますが、ローカルブラウザで表示されます。

custom header

+0

どのように具体的には機能しませんか?それはタイムアウトですか?エラーメッセージを出しますか?空の応答ですか? – Brian

+0

含まれるエラーに編集されました。 – mrcoulson

+0

[このリンク](http://www.productiverage.com/wcf-cors-plus-json-rest-complete-example)にチェックしましたか? –

答えて

0

は、AjaxのURLが正しいことを確認してくださいCustomersServiceのための構成は、次のようになります。

私のAjaxリクエストのURLは、サービスをホストしているマシンのFQDNではなく、localhostを参照しています。それが問題でした。もちろん、サービスがURLでホストされていない場合、Ajaxリクエストは機能しません。私は明らかに簡単な解決策を見逃してしまったというエラーメッセージに気を取られました。

1

使用JSONPはdifferenteドメーヌ(クロスオリジン・リクエスト共有)から作業するための最も古典的な標準です。 そして、追加制限のためにAccess-Control-Allow-Originを見てください。

JSONPを次のように使用します。 新しいJSONP機能は、WebHttpBindingを介して公開されています。

<bindings> 
    <webHttpBinding> 
     <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" /> 
    </webHttpBinding> 
    </bindings> 
    <services> 
    <service name="ServiceSite.CustomersService"> 
     <endpoint address="" binding="webHttpBinding" 
       bindingConfiguration="webHttpBindingWithJsonP" contract="ServiceSite.CustomersService" 
       behaviorConfiguration="webHttpBehavior"/> 
    </service> 
    </services> 

jQueryの

でJSONPを消費
// Get the JsonP data 
$.getJSON('http://localhost:65025/CustomersService.svc/GetCustomers?callback=?', null, function (customers) { 
     alert('Received ' + customers.length + ' Customers'); 
}); 

出典: How to natively enable JSONP for existing WCF service?

+0

これは、クライアントアプリケーションで '$ .ajax({})を置き換えますか?私は '$ .ajax({})'がウェブサービスと対話するのに好ましい方法だと考えました。 – mrcoulson

+0

これを試すと、 '' webHttpBehavior 'がそのデータ型' endpointBehaviorConfugurationType 'に従って無効であるというエラーが発生します。私はそれを実行すると、私はJSエラー "メソッドが許可されていない"を取得します。 – mrcoulson

+0

このドキュメントで試してください:http://www.bendewey.com/index.php/186/using-jsonp-with-wcf-and-jquery – ArDumez

関連する問題