我找不到跨域WCF jQuery AJAX调用的任何解决方案。因此,我在这里发布我如何解决此问题的信息。
在AJAX调用中使用GET方法时,无需提供数据。
在jQuery AJAX调用中使用WCF(跨域)时必须考虑的事项;
- 在Visual Studio的单独实例中运行WCF服务项目。不要将WCF服务项目和消耗项目混合在一个实例中并立即运行。运行消费项目时,WCF项目必须已经启动并正在运行。
- 使用DataType
jsonp
代替json
。 - 在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>



