C# 在visual studio开发服务器上为soap web服务启用CORS设置
我试图通过在如下所示的Web服务方法中启用“Access Control Allow Origin”头来添加CORS设置。但是,我仍然遇到错误:请求的资源上不存在“Access Control Allow Origin”头。“我是否缺少任何内容?”C# 在visual studio开发服务器上为soap web服务启用CORS设置,c#,asp.net,web-services,web,cors,C#,Asp.net,Web Services,Web,Cors,我试图通过在如下所示的Web服务方法中启用“Access Control Allow Origin”头来添加CORS设置。但是,我仍然遇到错误:请求的资源上不存在“Access Control Allow Origin”头。“我是否缺少任何内容?” [ScriptMethod(UseHttpGet = true)] [WebMethod] public ClientData[] GetClientData(int Number) {
[ScriptMethod(UseHttpGet = true)]
[WebMethod]
public ClientData[] GetClientData(int Number)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "http://localhost:52630");
ClientData[] Clients = null;
if (Number > 0 && Number <= 10)
{
Clients = new ClientData[Number];
for (int i = 0; i < Number; i++)
{
Clients[i].Name = "Client " + i.ToString();
Clients[i].ID = i;
}
}
return Clients;
}
[脚本方法(UseHttpGet=true)]
[网络方法]
公共客户端数据[]GetClientData(整数)
{
HttpContext.Current.Response.AddHeader(“访问控制允许源站”http://localhost:52630");
ClientData[]Clients=null;
如果(Number>0&&Number将其放入web.config
如果需要,你可以调整。这个例子我只为.aspx打开
<configuration>
<system.web>
<httpHandlers>
<add verb="GET,HEAD,POST,OPTIONS" path="*.aspx" type="System.Web.UI.PageHandlerFactory" />
</httpHandlers>
</system.web>
</configuration>
Chrome不支持CORS的localhost,请参阅:@Shiraz:到目前为止,它只在IE中工作,在Mozilla或Chrome中也不工作。@处理程序上的PoloHoleSet。基本思想是,我们需要拦截选项飞行前,并用适当的标题进行答复。这是我所怀疑的,但有时我的假设会让我误入歧途。谢谢大家的讨论回答了这么久才回来。
if (Request.HttpMethod == "OPTIONS")
{
Response.AppendHeader("Access-Control-Allow-Origin", "*");
Response.AppendHeader("Access-Control-Allow-Headers", "Content-Type");
return;
}