Search code examples
asp.net-web-apiasp.net-mvc-5corsjquery-ajaxq

Cross Domain issue while working with WebAPI


I have tried different approached but its not working for me.

XMLHttpRequest cannot load WEBAPI URL.

The 'Access-Control-Allow-Origin' header contains multiple values '*, CLIENT URL', but only one is allowed. Origin 'CLIENT URL' is therefore not allowed access.

this is the issue


Solution

  • Read this: CORS Your client must be allowed to do CORS, so you can use a placeholder like

    "*"

    to allow all clients, or explicitly by setting

    "www.YourAllowedClientDomain.com"

    in the "Access-Control-Allow-Origin" response header of your web api.

    for example:

    Access-Control-Allow-Origin: http://api.bob.com

    To make this work in Web API you have to configure it at startup.

    For example:

    public static class WebApiConfig {

    public static void Register(HttpConfiguration config)
    {
        var cors = new EnableCorsAttribute("https://client1.com,https://www.Client2.com", "*", "*");
        //To enable cross - origin requests, add the[EnableCors] attribute to your Web API controller or controller method:
        config.EnableCors(cors);
    

    Only one value is allowed!