0

I am using Postman to make POST request to API and save data to DB and I am getting as response {message:"Contact created successfully"}. BUT in Angular I don't get any response. What I am doing wrong?

I have provided a piece of my code below.

Angular Service

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

    };
    
    const contactApiUrl = "url to api/addContact.php";

    return this.http.post(contactApiUrl,contactItem, httpOptions).pipe(
            map( (response: any) => { console.log(response); }),
            catchError(this.handleError)
          );

  }

Contact.component.ts

  //here from the form I pass the data to service add()
  onSubmit(contactData){
     console.log(contactData);
     this.contactService.add(contactData).subscribe();

     //this.contactLst = this.contactService.get();
  }

addContact.php

//more code here

// create the product
if($contact->create()){
  
    // set response code - 201 created
    http_response_code(201);
  
    // tell the user
    echo json_encode(array("message" => "Contact was created."));
}
// if unable to create the contact, tell the user
else{
  
    // set response code - 503 service unavailable
    http_response_code(503);
  
    // tell the user
    echo json_encode(array("message" => "Unable to create contact."));
}

Any help is welcome.

2
  • Can you show where you call the add() method in the service? The caller needs to subscribe to the result in order to get the response. Do you see the call being made in the network tab of your browser's Dev Tools? In Chrome you can bring up the DevTools by pressing F12. Commented Sep 15, 2020 at 16:25
  • @harleybl I have added the code that you wanted Commented Sep 15, 2020 at 16:28

1 Answer 1

1

You're not actually returning the response. You are only logging it:

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

    };
    
    const contactApiUrl = "url to api/addContact.php";

    return this.http.post(contactApiUrl,contactItem, httpOptions).pipe(
            map( (response: any) => response ), // <- return response
            catchError(this.handleError)
          );

  }

To call it, you need to specify what happens in your subscribe callback:

//here from the form I pass the data to service add()
  onSubmit(contactData){
     console.log(contactData);
     this.contactService.add(contactData).subscribe( r => console.log(r) );

     //this.contactLst = this.contactService.get();
  }
Sign up to request clarification or add additional context in comments.

5 Comments

So in the service http.post the map function returns the response to the subscriber and prints it from there?
Yea, if that's how you want it. I'd imagine you'd do something with the response instead of just printing it to the console at some point.
You could also just make your add and onSubmit function async so your could just await the response instead of dealing with subscribers. It's personal preference though.
Can you show me an example with async in your answer?
I don't understand why handleError() function is implemented in service class and displays the errors ok but response must be printed in the subscriber().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.