How to add Access-Control-Allow-Origin?

后端 未结 1 2036

I am getting the following error when I call my webservice method.

Origin http://localhost:4165 is not allowed by Access-Control-Allow-Origin.
相关标签:
1条回答
  • 2020-12-30 18:05

    I have found the answer for my question. Just add the following to your web.config file

    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="Access-Control-Allow-Origin" value="*"/>
                <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
    

    Also if you dont want it to be set up globally, then you can add it to your action method alone as below:

    [WebMethod]
    public string HelloWorld()
    {
        HttpContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
        return "Hello User";
    }
    
    0 讨论(0)
提交回复
热议问题