xxxxxxxxxx
content_copy
getTextFile(filename: string) {
// The Observable returned by get() is of type Observable<string>
// because a text response was specified.
// There's no need to pass a <string> type parameter to get().
return this.http.get(filename, {responseType: 'text'})
.pipe(
tap( // Log the result or error
{
next: (data) => this.log(filename, data),
error: (error) => this.logError(filename, error)
}
)
);
}
xxxxxxxxxx
import { HttpClientModule } from '@angular/common/http';
// if you use services just import this
import { HttpClient } from '@angular/common/http';
//................................................
@NgModule({
imports: [
BrowserModule,
// import HttpClientModule after BrowserModule.
HttpClientModule,
],
// in you component.ts
readonly ROOT_URL = "https://jsonplaceholder.typicode.com";
constructor( private http: HttpClient) { }
this.http.get(this.ROOT_URL + '/posts')
.subscribe((response)=>{
alert(JSON.stringify(response));
});
// send information
this.http.post(this.ROOT_URL + '/posts',
{ id: 1, title: "sami"} )
.subscribe((response)=>{
alert(JSON.stringify(response));
});
// update
this.http.put(this.ROOT_URL + '/posts')
.subscribe((response)=>{
alert(JSON.stringify(response));
});
// fetch
// delete