Http.post not performing action


#1

So I am trying to pass a json object to my rest api which in turn will decode it and store it in my mysql database. The problem I am facing is that when I use the http.post function it does not pass my data or make the call to the site. I threw in a few console.logs so I know it is reaching the function but it is not getting past the post call.

I have tested it on my api side and it adds new data to the database when I pass in data through Chrome Poster so I do not believe the issue is there, plus I have echos in my api that will send messages confirming that a new entry was created or not.

I can oddly see those echos in my console when I use a http.get on my post url as well when I use an http.get on the post url it adds a new entry into my database. I am wondering if it may be an issue with my headers or if I am just using http.post incorrectly.

Here is where I make the http.post call. Data is an object made of data from user input.

  newAlert(data) {
    JSON.stringify(data);
    console.log(data);

    let myheaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    console.log("in new alert prior to post");
    return new Promise(resolve => {
      this.http.post(this.apiUrl+'/alerts/create.php', data, {headers: myheaders})
      .map((response: Response) => {
        console.log("in response");
        console.log(data);
        return response.json();
   
      });
    });
  }

#2

Can you show the screenshot of the response!!


#3

So this is what the console looks like when I run a get on the url. As you can see I get an error response and my message.

And this is what it looks like when using post where it does not look like it can get past the post.


#4

You should utilize toPromise on the Observable returned from http.post rather than explicitly creating an instance of Promise.

If for no other reason than you’re currently swallowing up any errors that occur.


#5

How would I go about doing such with my post call?

I have tried this but I am getting Uncaught in promise.

 this.http.post(this.apiUrl+'/alerts/create.php', data, {headers: myheaders})
        .toPromise()
          .then(
            res => {
              console.log(res);
            }
          );

#6

I got it working with this but I feel like I should be incorporating a promise into this function but would that be needed for a http.post?

      this.http.post(this.apiUrl+'/alerts/create.php', data, {headers: myheaders})
        .map(res => res)
          .subscribe(data => {
            console.log(data);
          });