0

Angular2 Get Request working fine but post request giving me error.this is my request

url = environment.API + "products/";
    headers = new Headers({ 'Content-Type': 'application/json' });
    options = new RequestOptions({ headers: this.headers });
    constructor(private http:Http) { 
    }
     getProductsList(data): Promise<any> {
        return this.http.post(this.url+'getproductsbyfilters', JSON.stringify(data), this.options).toPromise()
               .then(this.extractData)
                   .catch(this.handleErrorPromise);
    }
  • What is the error you're getting? – DesTroy Aug 11 '17 at 06:03
  • XMLHttpRequest cannot load http://localhost:8090/api/front/products/getproductsbyfilters. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. – Ali Nafees Aug 11 '17 at 06:08
  • looks like a CORS issue, allow the POST method to be accessed by a origin different than your server( your front end is on localhost:4200 and your api is on localhost:8090, two different origins ) – DesTroy Aug 11 '17 at 06:19
  • Yes but I have allowed that.... – Ali Nafees Aug 11 '17 at 06:23
  • app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.UseWebApi(httpConfig); var cors = new EnableCorsAttribute("http://localhost:4200", "*", "GET, HEAD, OPTIONS, POST, PUT"); cors.SupportsCredentials = true; httpConfig.EnableCors(cors); – Ali Nafees Aug 11 '17 at 06:24
  • https://stackoverflow.com/questions/34790051/how-to-create-cross-domain-request-angular-2 check on this. – DesTroy Aug 11 '17 at 06:31
  • I am using web api and I have done all but it is not working – Ali Nafees Aug 11 '17 at 06:36

1 Answers1

0

I added that in Web.config and it started working fine with that...

 <handlers>
        <remove name="OPTIONSVerbHandler"/>
</handlers>