Data on angular manner requested
get request:
1. The introduction of related modules and claim app.module.js
import {HttpClientModule} from '@ angualr / common / http'; // introduced module
imports: [// statement
HttpClientModule
]
2. Introduction and statement in the assembly
import {HttpClient} from '@angular/common/http' //引入
constructor(private http: HttpClient){ } //声明
3. Call function
ngOnInit () {
let url = 'route request'
// subsctibe: http encapsulated rxjs using a data acquisition request, similar to promise
this.http.get(url).subscribe((data:any)=>{
// data: data returned
})
}
post request:
1. The introduction of related modules and claim app.module.js
import {HttpClientModule} from '@ angualr / common / http'; // introduced module
imports: [// statement
HttpClientModule
]
2. Introduction and statement in the assembly
import {HttpClient,HttpHeaders,HttpParams} from '@angular/common/http' //引入
constructor(private http: HttpClient){ } //声明
3. Call function
ngOnInit () {
// set the request header
从上图httpOptions = {headers:new HttpHeaders({'Content-Type':'application/json'})}
let url = 'route request'
// subsctibe: http encapsulated rxjs using a data acquisition request, similar to promise
this.http.jsonp (url, 'callback function name or callback cb') subscribe ((data:. any) => {
// data: data returned
})
}
jsonp across request
1. The introduction of related modules and claim app.module.js
import {HttpClientModule,HttpClientJsonpModule} from '@angualr/common/http'; //引入模块
imports: [// statement
HttpClientModule,
HttpClientJsonpModule
]
2. Introduction and statement in the assembly
import {HttpClient} from '@angular/common/http' //引入
constructor(private http: HttpClient){ } //声明
3. Call function
ngOnInit () {
let url = 'route request'
// subsctibe: http encapsulated rxjs using a data acquisition request, similar to promise
this.http.get(url).subscribe((data:any)=>{
// data: data returned
})
}