在不同的项目消费WCF服务的jQuery通过AJAX调用(跨域) [英] Consuming a WCF Service in jQuery via AJAX Call in a different Project (Cross Domain)

查看:166
本文介绍了在不同的项目消费WCF服务的jQuery通过AJAX调用(跨域)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用,我已经下载了一个WCF jQuery的AJAX调用示例。我可以运行此并使其在同一个项目工作。当我访问同从一个不同的项目在同一溶液中,但不执行任何操作。下面就是该方法我打电话。

I'm using a WCF jQuery AJAX Call Sample which I have downloaded. I can run this and make it work in the same project. When I access the same from a different project in the same solution, it does nothing. The following is the method I'm calling.

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

我有警报()的在成功与失败的方法。

I have alert()'s in Success and Failure methods.

当我直接在浏览器中运行的网址,它返回我结果。 但是,如果我跑这从一个不同的项目,它什么都不做。没有提醒,没有结果。

When I run the URL directly in the browser, it returns me the result. But if I run this from a different project, it does nothing. No alerts, No results.

以下是我在哪里我的服务正在运行的项目的Web.Config;

Following is my Web.Config of the Project Where my Service is running;

<?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或任何错误的脚本? 我跟很多方法和尝试了各种方法。

Is there something to do with Web.config or anything wrong in the script? I have followed many methods and tried out various ways.

推荐答案

我无法找到一个跨域WCF的jQuery AJAX调用任何解决方案。因此,我张贴在这里,我是如何解决这个问题。

I could not find any solution for a cross domain WCF jQuery AJAX Call. Therefore, I'm posting here that how I have solved this issue.

有没有必要给数据时,我在Ajax调用使用GET方法。

There is no need to give data when I use GET method in AJAX Call.

在jQuery的AJAX调用(跨域)消耗WCF时,你必须考虑的事情;

The things that you must consider when consuming WCF in jQuery AJAX call (cross domain);

  1. 运行WCF服务项目在Visual Studio中的单独的实例。不要混合使用WCF服务项目和消费项目的一个实例,同时运行。当您运行的消费项目中,WCF项目必须已经启动并运行。
  2. 使用数据类型为 JSONP 而不是 JSON
  3. 在WCF项目的web.config文件,确保您有&LT属性 crossDomainScriptAccessEnabled =真正的;结合&GT; &LT标签\&LT;绑定&GT; \&LT;的WebHttpBinding&GT; 。另外,结合名称设置为 bindingConfiguration &LT属性;端点&GT; 标记
  1. Run the WCF Service Project in separate instance of Visual Studio. Do not mix WCF Service Project and Consuming Project in one instance and run at once. When you run the consuming project, the WCF Project must already be up and running.
  2. Use DataType as jsonp instead of json.
  3. In web.config of WCF Project, make sure you have the attribute crossDomainScriptAccessEnabled="true" in <binding> tag under <system.serviceModel>\<bindings>\<webHttpBinding>. Also the binding name set to bindingConfiguration attribute in the <endpoint> tag.

获得进一步的信息, 以下是我的jQuery AJAX调用;

For futher information, Following is my jQuery AJAX Call;

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

Following is my 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>

这篇关于在不同的项目消费WCF服务的jQuery通过AJAX调用(跨域)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆