O'Reilly logo

Learning Angular for .NET Developers by Rajesh Gunasundaram

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Posting to the Web API from TodoService

We just updated todo.Services.ts to call the Web API's GET action and fetch Todo items. Now, let's add code to post a new Todo item to the Web API. Follow the given steps to do so:

  1. Open todo.service.ts.
  1. Add the postTodo function that posts a new Todo item to the Web API controller:
     postTodo(todo: Todo): Promise<Todo> {              var headers = new Headers();              headers.append('Content-Type',       'application/json');         return this.http.post('/api/todos',       JSON.stringify(todo), { headers: headers })             .toPromise()             .then(response => response.json() as Todo)             .catch(this.handleError);      } 

This function accepts a Todo item as an argument. It defines the header section with the JSON content type and posts the Todo

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required