3

ダウンロードしたWCFjQueryAJAX呼び出しサンプルを使用しています。これを実行して、同じプロジェクトで機能させることができます。同じソリューションの別のプロジェクトから同じものにアクセスしても、何も起こりません。以下は私が呼び出しているメソッドです。

    function WCFJSON() {

        var parameter = "1234567890 (Cross Domain)";
        Type = "GET";
        Url = "http://localhost:52729/jQueryWebSite/Service.svc/Test?Id=" + parameter;
        Data = '{"Id": "' + parameter + '"}';
        ContentType = "application/json; charset=utf-8";
        DataType = "jsonp"; ProcessData = false;
        CallService();
    }

    $(document).ready(function () {
        WCFJSON();
    });

SuccessメソッドとFailureメソッドにalert()があります。

ブラウザで直接URLを実行すると、結果が返されます。しかし、これを別のプロジェクトから実行すると、何も起こりません。アラートなし、結果なし。

以下は、私のサービスが実行されているプロジェクトのWeb.Configです。

<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
    <compilation debug="true">
        <assemblies>
            <add assembly="System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
            <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            <add assembly="System.Data.Linq, Version=3.5.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/></assemblies>
    </compilation>
    <authentication mode="Windows"/>
</system.web>
<system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" warningLevel="4" type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <providerOption name="CompilerVersion" value="v3.5"/>
        <providerOption name="WarnAsError" value="false"/>
      </compiler>
    </compilers>
  </system.codedom>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceBehavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="true"/>
                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="EndpBehavior">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="ServiceBehavior" name="Service">
                <endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="EndpBehavior"/>
            </service>
        </services>
    </system.serviceModel>
</configuration>

Web.configと何か関係がありますか、それともスクリプトに何か問題がありますか?私は多くの方法に従い、さまざまな方法を試しました。

4

1 に答える 1

2

クロスドメインWCFjQueryAJAX呼び出しの解決策が見つかりませんでした。したがって、私はこの問題をどのように解決したかをここに投稿します。

AJAX呼び出しでGETメソッドを使用する場合、データを提供する必要はありません。

jQuery AJAX呼び出し(クロスドメイン)でWCFを使用するときに考慮しなければならないこと。

  1. VisualStudioの別のインスタンスでWCFサービスプロジェクトを実行します。WCFサービスプロジェクトと消費プロジェクトを1つのインスタンスに混在させて、一度に実行しないでください。消費プロジェクトを実行するときは、WCFプロジェクトが既に稼働している必要があります。
  2. jsonpの代わりにDataTypeを使用しますjson
  3. WCFプロジェクトのweb.configで、 。の下crossDomainScriptAccessEnabled="true"<binding>タグに属性があることを確認してください<system.serviceModel>\<bindings>\<webHttpBinding>。また、タグbindingConfigurationの属性に設定されたバインディング名。<endpoint>

詳細については、以下は私のjQueryAJAX呼び出しです。

$.ajax({
   cache: false,
   type: "GET",
   async: false,
   processData: true,
   data: "",
   url: "http://localhost:64973/Account.svc/GetAccountByID/2000",
   contentType: "application/json",
   dataType: "jsonp",
   success: function (result) {
       alert(result);
       alert(result.GetAccountByIDResult);
   }
});

以下は私のweb.configです。

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="crossDomain" crossDomainScriptAccessEnabled="true" />
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="tSeyvaWCFEndPointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="tSeyvaServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>
      <service name="tSeyva.WCF.Account" behaviorConfiguration="tSeyvaServiceBehavior">
        <endpoint address=""
                  behaviorConfiguration="tSeyvaWCFEndPointBehavior"
                  bindingConfiguration="crossDomain"
                  binding="webHttpBinding"
                  contract="tSeyva.WCF.IAccount">
        </endpoint>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>
于 2013-02-08T06:16:27.407 に答える