Can't read from server. It may not have the appropriate access-control-origin settings

11,580

Solution 1

Its an old question but I had same issue and didn't find the permanent fix but accidentally found the workaround. So sharing it here.

Swagger URL was http://localhost/blah/swagger/ui/index#/

But the URL for swagger doc was https://localhost/blah/swagger/docs/v1

Changing doc's URL from https to http worked.

If someone have any idea why in case of the above question didnt work even though both URLs have same protocol: https, please be kind to explain.

Solution 2

I just face the problem and this is my solution :

.EnableSwagger(c =>
                {
                    // By default, the service root url is inferred from the request used to access the docs.
                    // However, there may be situations (e.g. proxy and load-balanced environments) where this does not
                    // resolve correctly. You can workaround this by providing your own code to determine the root URL.
                    //
                    c.RootUrl(req =>
                    {
                        var url = req.RequestUri.Scheme ;
                       
    #if  !DEBUG 
                        url += "s";
    #endif
                        url += "://" + req.RequestUri.Authority + System.Web.VirtualPathUtility.ToAbsolute("~");
                        return url;
                    });
             });

Il permit to not use the https scheme in debug mode and use it when you release your app on a server. This solve the probleme for me.

Share:
11,580
Arjun A
Author by

Arjun A

Updated on June 16, 2022

Comments

  • Arjun A
    Arjun A almost 2 years

    I am getting the following error: Can't read from server. It may not have the appropriate access-control-origin settings. when the code is deployed to the server. I did a lot of research adding the Added following code in web.config

        <httpProtocol>
          <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
          </customHeaders>
        </httpProtocol>
    

    or in the startup project

    public static void Configuration(IAppBuilder app)
            {
                app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
            }
    

    or int the webapi.config -> Register

    config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
    

    Can some help? did anyone had the same issue

    enter image description here