[Angular] Send Data via HTTP using Angular HttpParams
2018-02-14 20:41
483 查看
Obviously in a real world application we do not only fetch data from the backend, but we also send data to be stored permanently on the server side. The
HttpClientgives us different options for achieving this. In this lesson we will look at how to attach parameters to our request URL, by manually concatenating the URL by ourselves, by using the
HttpParamsobject and also how we can send entire objects in a
POSTrequest body.
import { Injectable } from '@angular/core'; import { Observable } from 'rxjs/Observable'; import { HttpClient, HttpErrorResponse, HttpParams } from '@angular/common/http'; export interface Person { name: string; } @Injectable() export class PeopleService { constructor(private http: HttpClient) {} fetchPeople(): Observable<Person> { // The same as /api/v1/people?id=2&includeName=false const params = new HttpParams() .set('id', '2') .set('includeName', 'false'); return this.http .get<Person>('/api/v1/people', { params }); } }
相关文章推荐
- SAMPLE: Using HttpSendRequestEx for Large POST Requests
- How to get blob data using javascript XmlHttpRequest by sync
- angular-ui-router state.go not passing data to $stateParams
- Using Google Visualization API with own data soure(转http://www.jansipke.nl/using-google-visualization-api-with-own-data-source)
- [Project] Simulate HTTP Post Request to obtain data from Web Page by using Python Scrapy Framework
- Node(6) Accessing GET and POST data using http.ServerRequest
- Http client to POST using multipart/form-data
- Getting Data From the Web (URLConnection via http)
- How to use SSH Via HTTP Proxy using Corkscrew in Ubuntu
- HttpDataRequest & HttpDataProvider & lz.Params
- Transmitting Network Data Using Volley - Volley is an HTTP library that makes networking for Android
- Angular HttpClient upload file with FormData
- PostBot--Post Data via JavaME Http Connection
- [Angular 2] Using Pipes to Filter Data
- Part 17 Consuming ASP NET Web Service in AngularJS using $http
- How to send raw data to a printer by using Visual C# .NET
- [Angular] Fetch non-JSON data by specifying HttpClient responseType in Angular
- AngularJs: Detect unsaved data using angularjs
- httpclient via socks crawling the data
- [Angular 2] Using Promise to Http