9

This is my first experience with angular 2. I have created a simple form and try to submit it but when http.post is executed nothing happens. There is no request made in the network tab, there is not errors.

Here is my code:

 save(model) {
        var uri = this._baseUri + "/api/contact/AddContact";

        let md = JSON.stringify(model);

        this.http.post(uri,
            JSON.stringify(md),
            {
                headers: new Headers({
                    'Content-Type': 'application/json'
                })
            })
           .map(res => res.json());


    }

I have set a breakpoint on save method and is going through there but as I said nothing happens. What am I missing?

1 Answer 1

31

Observables are lazy so you need to subscribe on them to make the request execute even if you don't want to handle the response.

Something like that:

save(model) {
  var uri = this._baseUri + "/api/contact/AddContact";
  let md = JSON.stringify(model);

  this.http.post(uri,
    JSON.stringify(md),
    {
      headers: new Headers({
        'Content-Type': 'application/json'
      })
    })
    .map(res => res.json()).subscribe();
  }

Hope it helps you, Thierry

Sign up to request clarification or add additional context in comments.

2 Comments

Observables are really powerful ;-) You could start here regarding reactive programming: gist.github.com/staltz/868e7e9bc2a7b8c1f754. Really helpful to see their power
Thanks a ton! why didn't i found this post earlier. wasted couple of days on this. Cheers

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.