学习 JavaScript,我在本地计算机上运行 WCF 服务。 IIS版本是10。
我正在尝试通过在浏览器控制台中使用 JavaScript FETCH 来使用其中一种方法。承诺说“已履行”,但该方法不被允许。谷歌搜索显示它与 CORS 相关,因此我在网站标题中添加了一个标题以允许 (
Access-Control-Allow-Origin *
)。我无法编辑 WCF 的操作合同,因为我只是托管它们(测试服务)。
有什么办法可以看到数据吗?我可以使用 WCF 测试客户端访问服务和方法。
我也尝试过使用一些跨源代理,它可以工作,但是我如何看到像WCF测试客户端的输出这样的数据?
async function test(){
let s = await fetch('http://localhost/Main/Oriol.Host.BussServer/FinancialService.svc/GetAllTaxGroups',{
method: 'GET',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json'
}})).then(a => a.json());
console.log(s);
}
编辑:
这是界面...
<service name="Oriol.Financial.Service.FinancialService" behaviorConfiguration="OriolServiceBehavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" bindingConfiguration="restWebHttpBinding" contract="Oriol.Financial.Domain.ServiceInterfaces.IFinancialService" />
<endpoint address="soap" binding="basicHttpBinding" bindingConfiguration="OriolBasicHttpBinding" contract="Oriol.Financial.Domain.ServiceInterfaces.IFinancialService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint binding="netTcpBinding" bindingConfiguration="PortSharingBinding" contract="Oriol.Financial.Domain.ServiceInterfaces.IFinancialService" />
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
</service>
这是 FinancialService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="Oriol.Financial.Service.FinancialService" %>
错误:
GET http://localhost/Main/Oriol.Host.BussServer/FinancialService.svc/GetAllTaxGroups 405(不允许的方法)
未捕获(承诺中)语法错误:意外的标记 < in JSON at position 0
这是回复:
响应{类型:“cors”,网址:“http://localhost/Main/Oriol.Host.BussServer/FinancialService.svc/GetAllTaxGroups”,重定向:false,状态:405,确定:false,…}
身体:(...)
使用的身体:假
标题:标题 {}
好的:假
重定向:假
状态:405
statusText:“不允许使用方法”
类型:“cors”
网址:“http://localhost/Main/Oriol.Host.BussServer/FinancialService.svc/GetAllTaxGroups”
原型:响应
分辨率:
添加到配置文件中。
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type" />
</customHeaders>
此外,在 Fiddler 中仔细检查发现安全性只允许 POST。除了方法之外,我不需要任何其他标头参数。
一切顺利,谢谢大家。