クロスドメインAJAXを介した消費を許可するようにWCFサービスを構成するのに少し問題があります。
クライアントでこのサービスを問題なく使用できましたが、AJAXを使用して(jQueryの$ .ajaxオブジェクトを介して)ヒットしようとするたびに、400エラーが発生します。
これが私のweb.configです
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding" />
</basicHttpBinding>
<mexHttpBinding>
<binding name="mexHttpBinding" />
</mexHttpBinding>
<webHttpBinding>
<binding name="webHttpBinding" crossDomainScriptAccessEnabled="true">
<security mode="None" />
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="Services.WebAnalyticsService">
<clear />
<endpoint binding="basicHttpBinding" bindingConfiguration="basicHttpBinding"
name="WebAnalyticsDotNetClientEndpoint" contract="Contracts.IWebAnalyticsService"
listenUriMode="Explicit" />
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="mexHttpBinding"
name="WebAnalyticsMetaDataEndpoint" contract="Contracts.IWebAnalyticsService"
listenUriMode="Explicit" />
<endpoint address="script" behaviorConfiguration="aspNetAjaxBehavior"
binding="webHttpBinding" bindingConfiguration="webHttpBinding"
name="WebAnalyticsAjaxEndpoint" contract="Contracts.IWebAnalyticsService" />
<!--<endpoint address="web" behaviorConfiguration="RESTBehavior"
binding="webHttpBinding" bindingConfiguration="webHttpBinding"
name="WebAnalyticsAjaxEndpoint" contract="Contracts.IWebAnalyticsServiceWeb" />-->
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="aspNetAjaxBehavior">
<enableWebScript />
</behavior>
<!--<behavior name="RESTBehavior">
<webHttp helpEnabled="true"/>
</behavior>-->
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
<system.diagnostics>
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="xml"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData="c:\log\Traces.svclog" />
</listeners>
</source>
</sources>
</system.diagnostics>
</configuration>
そしてこれが私の運営契約です。
Namespace Contracts
<ServiceContract()>
Public Interface IWebAnalyticsService
<OperationContract(), WebGet(RequestFormat:=WebMessageFormat.Json)>
Sub SendWaEvent(ByVal eventID As Integer, ByVal eventValue As String, _
ByVal cookieVisitID As String, ByVal cookieVisitorSession As String, _
ByVal HTTPXForwardedServer As String, ByVal HTTPXRewriteURL As String, _
ByVal ScriptName As String, ByVal ServerName As String)
End Interface
End Namespace
私のAjax呼び出しは非常に簡単ですが、ここにあります:
$.ajax({
type: 'POST',
crossDomain: true,
url: 'http://localhost:37490/services/webanalyticservice.svc/SendWaEvent',
data: Data, //Data is a JSON wrapper I've previously constructed.
contentType: 'application/javascript;charset=UTF-8'
success: function(result) {DoSomething(result);},
error: HandleError
});
[以下の更新]
それはいつも私たちが見落としていることですよね?とにかく、上記の「単純な」AJAX呼び出しを非常に否定した後、それが問題になりました。これを機能させるには、AJAX呼び出しを次のように変更する必要がありました。
function SendAJAX() {
$.ajax({ type: "GET",
url: URL,
data: Data,
dataType: 'jsonp',
jsonpCallback: 'MyCallBack',
timeout: 10000,
crossDomain: true,
contentType: 'application/json; charset=UTF-8',
success: function (data, textStatus, jqXHR) { WriteSuccess(data, textStatus, jqXHR) },
error: function (jqXHR, textStatus, errorThrown) { WriteError(jqXHR, textStatus, errorThrown) },
complete: function (jqXHR, textStatus) { }
});
}
また、ローカルでテストする場合は、Web構成に次を追加する必要があります。そうしないと、認証されたサービスでクロスドメインJavaScriptが許可されていないという500エラーが表示されます。
<system.web>
<authentication mode="None" />
</system.web>
を使用して、web.release.configでこの属性を削除できますxdt:transform
。
@Rajeshに大声で叫ぶ!お疲れ様でした!