-1

I am working on an application using angular 4 and Web api. I am getting CORS error even I have kept code in Web.config , Start Up, WebApiConfig and on Controller as well but still i am getting this error. I don't to how to figure it out. Here is my code examples

Startup.cs

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

WebApiConfig

 var cors = new EnableCorsAttribute("*", "*", "*");
            config.EnableCors(cors);

Web.Config

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

Controller

 [EnableCors(origins: "*", headers: "*", methods: "*")]
    [ApiVersion1RoutePrefix("Settings")]
    public class SettingsController : ApiController

Even someone asked to remove this tag as well from web config and i also did this

<!--<remove name="OPTIONSVerbHandler" />-->

My angular call is here

  public getbyString<T>(apiUrl:any,param: string): Observable<T> {
    let _options = { headers: new HttpHeaders(
      { 
        'Content-Type': 'application/json',
        'Authorization': 'Bearer '+ localStorage.getItem('access_token')
      }
    )};
    return this.http.get<T>(this.actionUrl+apiUrl +"?version="+param,_options);
  }

Please see the below to see the error.

enter image description here

I have done this all but no success yet.

piet.t
  • 11,718
  • 21
  • 43
  • 52
Shamshad Jamal
  • 19
  • 3
  • 17

1 Answers1

-1

CORS (Cross-Origin Resource Sharing) is a way for the server to say "I will accept your request, even though you came from a different origin." This requires cooperation from the server – so if you can't modify the server (e.g. if you're using an external API), this approach won’t work.

Modify the server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from anywhere (or specify a domain instead of *).

Akash Gupta
  • 34
  • 10
  • "Modify the server to add the header Access-Control-Allow-Origin" — They appear to have done that. You can see it in the code in the question. – Quentin Dec 18 '18 at 10:59