1

I'm trying to send a post request from angular 2 to a php file.

Unfortunetly, when trying to var_dump($_POST), I see an empty array.

This is my angular 2 post:

addPerson(name)
{
    let body = JSON.stringify({
                name : name,    
        });
    this.http.request(this.BASE_URL + "mode=add", {body:body, method: 'POST'})
      .subscribe(
        response => {
            console.log(response.json());
        },
        error => {
          console.log(error.text());
        }
      );
  }

In my PHP File I just tried to var_dump($_POST). What am I missing?

1 Answer 1

1

I'm not sure why you used http.request. You could call HTTP post method directly, that seems simpler and more readable.

this.http.post(this.BASE_URL + "mode=add", body)
Sign up to request clarification or add additional context in comments.

3 Comments

I used http.request becaues angular 2 custom request options with fixed url doesn't work on the http.post method
What do you mean with "fixed url"?
export class CustomRequestOptions extends BaseRequestOptions { merge(options?:RequestOptionsArgs):RequestOptions { options.url = 'http://my-url.com' + options.url; if (options.method === 1) { let headers = new Headers(); headers['Content-Type'] = 'application/json'; headers.append('Content-Type', 'application/json'); options.headers = headers; } return super.merge(options); } } and I have the same output (empty array) with the this.http.post method

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.