0

My problem is in my api Angular 6. Once the interceptors are created when send POST request these become in OPTIONS request.

What is the problem ?

This is the code of one Service

import { Injectable } from '@angular/core';
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { AppConfig } from '../app.config';
import { Perfil } from '../Entities/Perfil';
// import { Http } from '@angular/http';

@Injectable({
  providedIn: 'root'
})
export class UsuariosService {

  constructor(private http: HttpClient, private config: AppConfig) { }

  private per = new Perfil(1);

  getPerfiles() {
    return this.http.post(this.config.apiUrl + 'perfiles/Buscar/', this.per);
  }


}

this image will help us in this problem:

enter image description here

Regards!

Alexis Zapata
  • 703
  • 1
  • 5
  • 8
  • 2
    `OPTIONS` is the special request method when you trying to access resources from different domain than you server. It involves CORS. Please read more here - [Why is an OPTIONS request sent and can I disable it?](https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it) – lomboboo Jun 07 '18 at 13:35

3 Answers3

1

This is a CORS issue and like lomboboo said OPTIONS is a special request made to server to ask what can I do and am I allowed to access content?

In order to provide the fix, in your server (backend) you need to implement CORS headers and allow for POST and any other operations you need.

Mike Tung
  • 4,242
  • 1
  • 12
  • 20
1
//**need to pass header in your post api**


const httpOptions = {
  headers: new HttpHeaders({ 'Content-Type': 'application/json' })
};

 getPerfiles() {
    return this.http.post(this.config.apiUrl + 'perfiles/Buscar/', this.per,httpOptions).subscribe(result => {
        console.log(result);
      }, error => console.log('There was an error: '));


};
  }
mittal bhatt
  • 841
  • 6
  • 12
0

Yeahhh! . I knew that they were the cors, and did implement in the back, but bad. The solutions i whas implement is this: (using .Net Framework 4 )

Inside of WebApiConfig.cs

public static void Register(HttpConfiguration config)
        {

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

thanks lomboboo and Mike, were this very help!

Alexis Zapata
  • 703
  • 1
  • 5
  • 8