栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在另一个项目(跨域)中通过AJAX调用在jQuery中使用WCF服务

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在另一个项目(跨域)中通过AJAX调用在jQuery中使用WCF服务

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

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

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

  1. 在Visual Studio的单独实例中运行WCF服务项目。不要将WCF服务项目和消耗项目混合在一个实例中并立即运行。运行消费项目时,WCF项目必须已经启动并正在运行。
  2. 使用DataType
    jsonp
    代替
    json
  3. 在WCF项目的web.config文件,确保您有属性
    crossDomainscriptAccessEnabled="true"
    <binding>
    下标记
    <system.serviceModel><bindings><webHttpBinding>
    。还将绑定名称设置为标记中的
    bindingConfiguration
    attribute
    <endpoint>

有关更多信息,以下是我的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>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406795.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号