
###前提・問題点
Microsof Visual Studio Community 2017で開発しております。
Windows 2012 ServerにWCFサービスを配置し、IISで公開しています。
'Access-Control-Allow-Origin'ヘッダが無いため401(Unauthorized)が返るとのことで、Ajaxから呼び出すことができません。
(下記ソースコードの、WCFサービスにおけるGlobal.asaxがうまく読み込めていないためかと考えているのですが、)どのように修正したらよいでしょうか。また、気をつけるべき設定はあるでしょうか。
なお、URLがドメインを含んでいるのは、Cordovaでモバイルアプリ化を図ろうとしているためです。クロスドメインでも呼び出せるようにGlobal.asaxファイルを設けました。
###エラーメッセージ
Chromeの開発ツールで確認すると、以下のようなメッセージが出ています。
- Failed to load resource:the server responded with a status of 401(Unauthorized)
- No 'Access-Control-Allow-Origin' header is present on the requested resource.
JAVASCRIPT
1 function SetselCate() { 2 var iData = {}; 3 iData.Cate = 1; 4 5 $.ajax({ 6 // url: '<%=ResolveUrl("~/SyouBu.svc/SetselCate")%>', //これは用いない 7 url: 'http://*********.jp/SHOPserv/SyouBu.svc/SetselCate', 8 type: 'GET', 9 dataType: 'json', 10 contentType: "application/json; charset=utf-8", 11 data: iData, 12 success: function (response) { 13 var cateset = JSON.parse(response.d); 14 $.each(cateset, function () { 15 var setCateID = this.cateid; 16 var setCateNM = this.catenm; 17 }); 18 }, 19 error: function (xhr, status, err) { 20 console.log(xhr); 21 console.log(status); 22 console.log(err); 23 alert('通信失敗'); 24 25 } 26 }); 27 };
WCFサービスです。ファイル名はWcfService1です。
VBNET
1Imports System.ServiceModel 2Imports System.ServiceModel.Activation 3Imports System.ServiceModel.Web 4Imports System.Web.Script.Serialization 5 6<ServiceContract(Namespace:="")> 7<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> 8Public Class SyouBu 9 <WebGet()> 10 Public Function SetselCate(ByVal Cate As String) As String 11 Dim syoubu As New List(Of Object)() 12 syoubu.Add(New With { 13 Key .cateid = "カテゴリIDの返り値", 14 Key .catenm = "カテゴリ名の返り値" 15 }) 16 Return (New JavaScriptSerializer().Serialize(syoubu)) 17 End Function 18End Class
WCFサービスのGlobal.asaxです。
VBNET
1Imports System.Web.SessionState 2 3Public Class Global_asax 4 Inherits System.Web.HttpApplication 5 Protected Sub Application_BeginRequest(sender As Object, e As EventArgs) 6 HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache) 7 HttpContext.Current.Response.Cache.SetNoStore() 8 EnableCrossDmainAjaxCall() 9 End Sub 10 11 Private Sub EnableCrossDmainAjaxCall() 12 HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*") 13 14 If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then 15 HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST") 16 HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept") 17 HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000") 18 HttpContext.Current.Response.[End]() 19 End If 20 End Sub 21End Class
Web.configの一部です。
XML
1 <system.serviceModel> 2 <behaviors> 3 <endpointBehaviors> 4 <behavior name="WcfService1.SyouBuAspNetAjaxBehavior"> 5 <enableWebScript /> 6 </behavior> 7 </endpointBehaviors> 8 9 <serviceBehaviors> 10 <behavior name="ajaxServiceBehavior"> 11 <serviceDebug includeExceptionDetailInFaults="true" /> 12 </behavior> 13 <behavior name=""> 14 <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" /> 15 <serviceDebug includeExceptionDetailInFaults="false" /> 16 </behavior> 17 </serviceBehaviors> 18 </behaviors> 19 20 <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> 21 22 <services> 23 <service name="WcfService1.SyouBu"> 24 <endpoint address="" behaviorConfiguration="WcfService1.SyouBuAspNetAjaxBehavior" binding="webHttpBinding" contract="WcfService1.SyouBu" /> 25 </service> 26 </services> 27 28 </system.serviceModel>
###試したこと
同一ドメインであれば問題なく期待通りの結果が得られていると思います。
開発環境(Windows10)のIISにWCFサービスを配置して試したところ、エラーは出ませんでした。
ローカルのIPアドレスが192.168.0.108 なので、AjaxのURLを以下のように書きました。
url: 'http://192.168.0.108/SHOPserv/SyouBu.svc/SetselCate',





回答1件
あなたの回答
tips
プレビュー