Oblivion
Oblivion

Reputation: 635

Angular HttpClient post not sending

Probably just a silly mistake somewhere but i cant seem to pinpoint the cause. The function is being called (verified by the console log) but for some reason neither the error log or the success log print out afterwards. The back end is not receiving the post request, so why is the error log not showing anything?

getBooks(): Observable <FormData | null> {
  let request = new FormData();
  request.append('action', "GetBooks");

  console.log("Function has been called");

  return this.http.post('http://localhost/Projects/Website/PHP/index.php', request).pipe(
    map((returned) => {
      console.log("Here I parse 'returned'");

      let books = new FormData();

      return books;
  }, (error) => {
    console.log('Error! ', error);
    return null;
  }));
}

Upvotes: 0

Views: 342

Answers (1)

Or Yaacov
Or Yaacov

Reputation: 3880

you need to call .subscribe() so your method will be used.

since your work with an observable, subscribe is the function that "listen" to any data coming from the observable, which actually execute the code.

     var res=null;
     this.http.post('http://localhost/Projects/Website/PHP/index.php', 
     request).subscribe(data => {
          res=data;
          },
          error => {
           ...
          },
          () => {
          ...
          }
          return res;

Upvotes: 3

Related Questions