【问题标题】:Consuming a WCF Service in jQuery via AJAX Call in a different Project (Cross Domain)在不同的项目(跨域)中通过 AJAX 调用在 jQuery 中使用 WCF 服务
【发布时间】:2013-01-13 05:24:41
【问题描述】:

我正在使用我下载的 WCF jQuery AJAX 调用示例。我可以运行它并让它在同一个项目中工作。当我从同一解决方案中的不同项目访问相同内容时,它什么也不做。以下是我正在调用的方法。

    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();
    });

我在成功和失败方法中有 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 或脚本中的任何错误有关? 我遵循了很多方法,尝试了各种方法。

【问题讨论】:

  • 确保您的绑定启用了 crossDomainScriptAccessEnabled="true"。
  • 自 jquery 1.9 起,失败方法不能与 jsonp 结果一起使用(也许是 1.8?我不记得了)。如果发生错误,它将显示为类似于使用 throw "OOPS!" 创建的 js 错误。我不熟悉您使用的服务器端语言。它是否正确检测到您想要 JSONP 而不是 JSON 并返回 JSONP? JSONP != JSON
  • 哦,我明白了。因为,如果我在跨域调用中使用 json,它会返回 throws Method Not Allowed
  • 当我在 中写 crossDomainScriptAccessEnabled="true" 时,它用蓝色下划线表示“不允许使用 'crossDomainScriptAccessEnabled' 属性。这是为什么?
  • 使用 jQuery 时,如果它似乎什么都不做,请使用 fiddler 或开发工具检查是否正在发出请求。如果甚至没有发出请求,那么 jsonp 可能无法正常工作,并且 jQuery 可能已确定您的浏览器未启用 COR(这也将在 IE 的兼容模式下发生)。如果不允许使用crossDomainScriptAccessEnabled,您可能使用的是低于 4.0 的 .NET 版本

标签: jquery ajax json wcf


【解决方案1】:

我找不到跨域 WCF jQuery AJAX 调用的任何解决方案。因此,我在这里发布我是如何解决这个问题的。

在 AJAX 调用中使用 GET 方法时无需提供数据。

在jQuery AJAX调用(跨域)中使用WCF时必须考虑的事项;

  1. 在单独的 Visual Studio 实例中运行 WCF 服务项目。不要在一个实例中混合 WCF 服务项目和消费项目并立即运行。当您运行消费项目时,WCF 项目必须已经启动并运行。
  2. 使用 DataType 作为jsonp 而不是json
  3. 在 WCF 项目的 web.config 中,确保 &lt;system.serviceModel&gt;\&lt;bindings&gt;\&lt;webHttpBinding&gt; 下的 &lt;binding&gt; 标记中有 crossDomainScriptAccessEnabled="true" 属性。还将绑定名称设置为&lt;endpoint&gt; 标记中的bindingConfiguration 属性。

如需更多信息, 以下是我的 jQuery AJAX 调用;

$.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>

【讨论】:

  • 我不认为您有任何机会可以将链接放置到工作的 VS 项目以供下载吗?我正在尝试将 WCF 部署到 azure(云服务)并从单独的域中调用它并且真的很挣扎。
猜你喜欢
  • 2012-01-28
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 1970-01-01
  • 2012-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多