1

i am trying to add CORS setting by enabling "Access-Control-Allow-Origin" header in Web Service method shown below. However, I'm still getting error: No 'Access-Control-Allow-Origin' header is present on the requested resource.' Am i missing anything?

        [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;
        }
user3399326
  • 863
  • 4
  • 13
  • 24

1 Answers1

2

Put this in your web.config

You can adjust if needed. This example I only open for .aspx

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

You may need something like this too.

    if (Request.HttpMethod == "OPTIONS")
    {
        Response.AppendHeader("Access-Control-Allow-Origin", "*");
        Response.AppendHeader("Access-Control-Allow-Headers", "Content-Type");
        return;
    }
TLJ
  • 4,525
  • 2
  • 31
  • 46
  • @PoloHoleSet On your handler. The basic idea is that, we need to intercept the option preflight and reply with appropriate headers. – TLJ Apr 04 '18 at 16:12
  • That's what I suspected, but sometimes my assumptions lead me astray. Thanks for circling back so long after answering. – PoloHoleSet Apr 04 '18 at 16:42