0

クロスドメイン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に大声で叫ぶ!お疲れ様でした!

4

1 に答える 1

1

JQuery 関数は、次のようにする必要があります。

function TestingWCFRestWithJsonp() {
                $.ajax({
                    url: "http://localhost:37490/services/webanalyticservice.svc/script/SendWaEvent",
                    dataType: "jsonp",
                    type: "GET",
                    timeout: 10000,
                    jsonpCallback: "MyCallback",
                    success: function (data, textStatus, jqXHR) {
                    },
                    error: function (jqXHR, textStatus, errorThrown) {    
                    },
                    complete: function (jqXHR, textStatus) {
                    }
                });
            }
            function MyCallback(data) {
                alert(data);
            }

エンドポイント要素にはアドレス値が設定されているため、それを .svc の末尾に追加し、以下のサンプルのようにメソッド名を指定する必要があります。

binding 要素の crossDomainScriptAccessEnabled プロパティは、要求にコールバック メソッドが指定されているかどうかを識別し、応答をストリームに書き戻します。

注: ブラウザーから URL をテストして、WebGet メソッドとして成功した応答が得られるかどうかを確認します。

于 2012-08-17T09:57:49.843 に答える