Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 804
Arjun A Avatar asked Sep 05 '17 18:09

Arjun A


2 Answers

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.

like image 132
kay Avatar answered Oct 18 '22 21:10

kay


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.

like image 20
Eliott Roynette Avatar answered Oct 18 '22 19:10

Eliott Roynette