【发布时间】:2015-12-26 10:46:54
【问题描述】:
在我的解决方案文件中,我添加了两个项目,一个是 WCFService 应用程序,另一个是空的 MVC 应用程序。
在 WCF 服务中。
public interface IProductService
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/ProductName/{productID}",
BodyStyle = WebMessageBodyStyle.Bare,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
string GetProductName(string productID);
}
WCF 配置文件如下
<system.serviceModel>
<services>
<service behaviorConfiguration="Default"
name="RESTFulWCFService.ProductService">
<endpoint address="" behaviorConfiguration="webBehavior"
binding="webHttpBinding" contract="RESTFulWCFService.IProductService"></endpoint>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"></endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true"/>
</system.webServer>
现在,当我使用 jquery 调用 WCF 时,在浏览器控制台中出现此错误。
**Error: Permission denied to access property "apply"**
我的 Jquery 代码
<script>
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://localhost:55827/ProductService.svc/ProductName/2",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
success: function (result) {
debugger;
alert(result.d);
console.log(result);
},
error: function (result) {
debugger;
alert('no');
}
});
});
</script>
【问题讨论】:
-
为什么不在 MVC 项目中添加服务引用?
-
它是WCF restfull服务,那么为什么我需要在我的Web中引用WCF,什么时候可以使用ajax调用它。
标签: c# jquery asp.net ajax wcf