0

Trying to hit a WEB API through Angular http post method but unable to hit the web api and catch block is not throwing any error as well. I have enabled cross-origin policy as well. When I paste the web api url in th browser then it hits the method but not through angular http post function.

Below is the code component code

    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });  

    this._http.post(this._commonUrl, filter, options)
        .map(response => response.json())
        .catch(this.handleError);


     private handleError(error: Response) {
        console.error(error);
        return Observable.throw(error.json().error || 'Server error');
     }

Controller code

[RoutePrefix("api/common")]
public class ProductController : ApiController
{
    public ProductController()
    {

    }

    [Route("initialize")]
    [HttpPost]
    public HttpResponseMessage SearchLazada(Filter filter)
    {
        try
        {
        }
    }
}
user728630
  • 2,025
  • 5
  • 22
  • 35
  • Possible duplicate of [Angular 2 http get not getting](https://stackoverflow.com/questions/41381200/angular-2-http-get-not-getting) – AT82 Jun 03 '17 at 09:12

2 Answers2

1

Your post request will not be even raised. Look in the network calls.

  • You need to subscribe to it.

    this._http.post(this._commonUrl, filter, options)
        .map(response => response.json())
        .catch(this.handleError)
        .subscribe(data =>console.log(data));
    
  • You can use the error call back to access the error

    this._http.post(this._commonUrl, filter, options)
        .map(response => response.json())
        .catch(this.handleError)
        .subscribe(data =>console.log(data),
        (error)=>console.log(error));//////////////////////////
    
Aravind
  • 40,391
  • 16
  • 91
  • 110
0

With Content-Type : application/json , you should transform body data to json.

filter : JSON.stringify(data)

Thien Hoang
  • 625
  • 3
  • 12