【发布时间】:2014-11-11 10:22:43
【问题描述】:
您好,我有一个 WCF Rest 服务,使用的是(WCF REST 服务模板 40(CS))。我设法让它返回 Json 响应。项目运行时工作正常。但是当我尝试从浏览器对服务进行 Ajaxcall 时,出现错误:
跨域请求被阻止:同源策略不允许读取
http://192.168.0.70:8001/Service/test的远程资源。 这可以通过将资源移动到同一域或 启用 CORS。
还有 Ajax 调用:
$.ajax({
type: "POST",
url: "http://192.168.0.70:8001/Service/test",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
cache: false,
success: function (msg) {
var Tabels = msg.d;
alert('success');
}
});
这是 web.config:
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ConString" connectionString="SERVER=localhost;DATABASE=fabapp;UID=root;PASSWORD=;"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<modules runAllManagedModulesForAllRequests="true">
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
</configuration>
我尝试添加 crossDomainScriptAccessEnabled="true" 但是当我这样做时,该服务也无法在本地主机上运行。我明白了:
认证不支持跨域javascript回调 服务。
我需要在 web.config 文件中更改什么?
【问题讨论】:
-
你试过我的解决方案了吗?
标签: c# asp.net web-config wcf-rest