【发布时间】:2026-01-26 19:35:01
【问题描述】:
我创建了一个简单的 C# WCF 服务,它返回一个带有 html 代码的字符串。当我在 WCF 解决方案中通过一个简单的 MVC 项目使用此服务时,一切正常。
- 服务代码
public class ConnectorService : IConnectorService
{
public string GetData()
{
return "<a href='www.test.com.br'>test</a>";
}
}
- 接口代码
[ServiceContract]
public interface IConnectorService
{
[OperationContract]
string GetData();
}
在此测试之后,我在本地 IIS 中发布了此服务,并尝试使用不在 WCF 解决方案中但位于服务的同一 IIS 目录中的 html 页面来使用此服务。
- HTML 代码
<html>
<head>
<title>test</title>
<script src='Scripts/jquery-3.3.1.min.js'></script>
</head>
<body>
<div id='divContent'></div>
</body>
</html>
<script type='text/javascript'>
$(document).ready(function () {
$.ajax({
url: 'http://localhost/ConnectorService.svc/GetData',
contentType: 'application/json; charset=utf-8',
type: "GET",
dataType: 'jsonp',
success: function (data) {
$('#divContent').html(data);
},
error: function (error) {
alert("error: status - " + error.status + " | text: " + error.statusText);
}
});
});
</script>
当我在浏览器上打开这个 html 文件时,出现 2 个错误:
1) CORS 政策 - 我用这样的 global.asax 文件修复了这个问题
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
2) 错误 400 - 错误请求 我尝试了几种堆栈溢出的解决方案,通常会更改我的 ajax 调用、global.asax 和 web.config 文件,但我总是在 chrome 控制台中收到错误的请求错误。
- web.config 代码
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.7.2" />
<httpRuntime targetFramework="4.7.2"/>
</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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Credentials" value="true" />
<add name="Access-Control-Allow-Headers" value="Content-Type,Accept" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" />
</customHeaders>
</httpProtocol>
<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>
我相信这是一个简单的解决方案,但是经过几天的测试,我觉得我在追逐自己的尾巴。有人能指出我的解决方案吗?
提前致谢。
【问题讨论】:
-
控制器代码在哪里?它甚至被击中了吗?你试过调试吗?
-
@Austin T French,正如我所说,在我的 MVC 代码中一切正常,问题是当我在 IIS 上发布我的服务并尝试使用独立于我的 MVC 项目的 html 文件访问它时。在这种情况下,没有要调试的控制器,因为我的 ajax 调用是在 html 代码中硬编码的。
-
@Mark 这只发生在
Chrome上吗?您在控制台上得到的详细错误是什么? -
@Rahul Sharma,Firefox 和 IE 也会出现此错误。详细的控制台错误是:jquery-3.3.1.min.js:2 GET localhost/ConnectorService.svc/… net::ERR_ABORTED 400 (Bad Request)