我想用我的代码在我的Mvc应用程序中调用我的WCF服务:
function SaveRouteCreation()
{
var serviceURL = 'http://localhost:36544/Service1.svc/GetData';
$.ajax({
type: "POST",
url: serviceURL,
data: param = "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: successFunc,
error: errorFunc
});
}
现在这是我的WCF服务项目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>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- 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>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</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>
现在如何在我的Mvc项目中调用我的WCF服务?
步骤1:在我的Mvc应用程序中添加Wcf项目的服务参考:
点击对话框1
现在点击添加网页参考
“添加Web引用”对话框:
步骤2:是否可以在没有添加服务引用的情况下使用Web.config设置直接在mvc项目中调用我的wcf服务(抱歉,我不知道这一点,只是猜测)。
当我没有添加服务引用并使用AJAX在按钮点击事件上调用我的wcf服务时,我在Firefox中收到此错误:
阻止跨源请求:同源策略禁止在http://localhost:36544/Service1.svc/GetData读取远程资源。 (原因:CORS请求失败)。
我在互联网上搜索但是找不到任何有用的东西,甚至尝试了一些解决方案,如果它不是交叉起源,但它没有用。