2015-07-21 69 views
1

我想通过在下面显示的Web服务方法中启用“Access-Control-Allow-Origin”头来添加CORS设置。但是,我仍然收到错误消息:没有“Access-Control-Allow-Origin”标题出现在请求的资源上。我错过了什么?在Visual Studio开发服务器上为soap web服务启用CORS设置

 [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; 
     } 
+0

Chrome不支持本地主机的CORS,请参阅:http://stackoverflow.com/questions/10883211/deadly-cors-when-http-localhost-is-the-origin –

+0

@Shiraz:到目前为止,只适用于IE,不适用于Mozilla或Chrome。 – user3399326

回答

1

在你的web.config

如果需要,可以调整将这个。这个例子我只打开的.aspx

<configuration> 
    <system.web> 
     <httpHandlers> 
     <add verb="GET,HEAD,POST,OPTIONS" path="*.aspx" type="System.Web.UI.PageHandlerFactory" /> 
     </httpHandlers> 
    </system.web> 
</configuration> 

您可能需要像这样过。

if (Request.HttpMethod == "OPTIONS") 
    { 
     Response.AppendHeader("Access-Control-Allow-Origin", "*"); 
     Response.AppendHeader("Access-Control-Allow-Headers", "Content-Type"); 
     return; 
    }