我正在使用我下载的WCF jQuery AJAX Call Sample。我可以运行它并使它在同一个项目中工作。当我从同一解决方案中的不同项目访问相同的内容时,它什么都不做。以下是我打电话的方法。
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();
});
我有成功和失败方法的警报()。
当我直接在浏览器中运行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或脚本中的任何错误有关? 我遵循了很多方法并尝试了各种方法。
答案 0 :(得分:2)
我找不到任何跨域WCF jQuery AJAX调用的解决方案。因此,我在这里发帖说明我是如何解决这个问题的。
在AJAX Call中使用GET方法时无需提供数据。
在jQuery AJAX调用(跨域)中使用WCF时必须考虑的事项;
jsonp
而不是json
。crossDomainScriptAccessEnabled="true"
下的<binding>
标记中包含属性<system.serviceModel>\<bindings>\<webHttpBinding>
。此外,绑定名称在bindingConfiguration
标记中设置为<endpoint>
属性。有关更多信息, 以下是我的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;
<?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>