【发布时间】:2016-02-11 16:57:05
【问题描述】:
当我尝试直接从 Web 应用程序访问托管在 Windows 服务中的 WCF 服务时遇到问题,我无法理解我做错了什么。
我尝试了所有我发现但没有任何帮助的建议。我使用 AngularJs,但这并不重要,我接受所有建议。
有我的项目:https://github.com/djromix/Portal.WorckFlow
Portal.Services 是 Windows 服务。
这是我的 Windows 服务配置:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Portal.Services.ServiceContract.PortalContract">
<endpoint
address=""
binding="basicHttpBinding"
contract="Portal.Services.ServiceContract.IPortalContract">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8000/Portal" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
服务代码:
namespace Portal.Services.ServiceContract
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IPortalContract" in both code and config file together.
[ServiceContract(Namespace = "")]
public interface IPortalContract
{
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
double Ping();
[OperationContract]
object CashInResult(string key);
}
}
namespace Portal.Services.ServiceContract
{
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class PortalContract : IPortalContract
{
public double Ping()
{
return -1;
}
[WebGet]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
public object CashInResult(string key)
{
return new {Value = "Some Data"};
}
}
}
我只想简单地访问 url 并获取 json 结果
http://localhost:8000/Portal/CashInResult?key=secretkey
现在我收到错误[Failed to load resource: the server responded with a status of 400 (Bad Request)]
从网络应用程序我得到错误
XMLHttpRequest cannot load /Portal/CashInResult?key=1. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '???' is therefore not allowed access. The response had HTTP status code 400.
【问题讨论】:
-
您尝试了哪些方法,结果如何?你能看到服务吗?你有错误吗?
-
从浏览器访问 url 时出现异常 [加载资源失败:服务器响应状态为 400 (Bad Request]