angular-httpclient

Angular 5 caching http service api calls

冷暖自知 提交于 2019-11-26 18:19:09
问题 In my Angular 5 app a certain dataset (not changing very often) is needed multiple times on different places in the app. After the API is called, the result is stored with the Observable do operator. This way I implemented caching of HTTP requests within my service. I'm using Angular 5.1.3 and RxJS 5.5.6. Is this a good practise? Are there better alternatives? import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs

Angular 4/5 HttpClient: Argument of type string is not assignable to 'body'

守給你的承諾、 提交于 2019-11-26 17:43:51
问题 The Angular docs say: The response body doesn't return all the data you may need. Sometimes servers return special headers or status codes to indicate certain conditions, and inspecting those can be necessary. To do this, you can tell HttpClient you want the full response instead of just the body with the observe option: http .get<MyJsonData>('/data.json', {observe: 'response'}) .subscribe(resp => { // Here, resp is of type HttpResponse<MyJsonData>. // You can inspect its headers: console.log

Angular HttpClient “Http failure during parsing”

帅比萌擦擦* 提交于 2019-11-26 17:34:47
I try to send an POST request from Angular 4 to my Laravel backend. My LoginService has this method: login(email: string, password: string) { return this.http.post(`http://10.0.1.19/login`, { email, password }) } I subscribe to this method in my LoginComponent: .subscribe( (response: any) => { console.log(response) location.reload() }, (error: any) => { console.log(error) }) And this is my Laravel backend method: ... if($this->auth->attempt(['email' => $email, 'password' => $password], true)) { return response('Success', 200); } return response('Unauthorized', 401); My Chrome dev tools says

Angular 4 HttpClient Query Parameters

半城伤御伤魂 提交于 2019-11-26 11:48:49
问题 I have been looking for a way to pass query parameters into an API call with the new HttpClientModule \'s HttpClient and have yet to find a solution. With the old Http module you would write something like this. getNamespaceLogs(logNamespace) { // Setup log namespace query parameter let params = new URLSearchParams(); params.set(\'logNamespace\', logNamespace); this._Http.get(`${API_URL}/api/v1/data/logs`, { search: params }) } This would result in an API call to the following URL: localhost

Property &#39;json&#39; does not exist on type &#39;Object&#39;

自作多情 提交于 2019-11-26 08:18:14
问题 I\'m trying fetch data via REST with angular 2 HttpClient. I\'m following the angular tutorial here https://angular.io/tutorial/toh-pt6 and under the Heroes and HTTP section you\'ll see this snippet of code used to fetch hero data via http. getHeroes(): Promise<Hero[]> { return this.http.get(this.heroesUrl) .toPromise() .then(response => response.json().data as Hero[]) .catch(this.handleError); } And below is a similar version I wrote in my application fetch(startIndex: number, limit: number)

Angular HttpClient “Http failure during parsing”

耗尽温柔 提交于 2019-11-26 05:27:34
问题 I try to send an POST request from Angular 4 to my Laravel backend. My LoginService has this method: login(email: string, password: string) { return this.http.post(`http://10.0.1.19/login`, { email, password }) } I subscribe to this method in my LoginComponent: .subscribe( (response: any) => { console.log(response) location.reload() }, (error: any) => { console.log(error) }) And this is my Laravel backend method: ... if($this->auth->attempt([\'email\' => $email, \'password\' => $password],

Difference between HttpModule and HttpClientModule

血红的双手。 提交于 2019-11-26 03:17:12
问题 Which one to use to build a mock web service to test the Angular 4 app? 回答1: Use the HttpClient class from HttpClientModule if you're using Angular 4.3.x and above: import { HttpClientModule } from '@angular/common/http'; @NgModule({ imports: [ BrowserModule, HttpClientModule ], ... class MyService() { constructor(http: HttpClient) {...} It's an upgraded version of http from @angular/http module with the following improvements: Interceptors allow middleware logic to be inserted into the

Adding a HTTP header to the Angular HttpClient doesn&#39;t send the header, why?

▼魔方 西西 提交于 2019-11-26 01:23:28
问题 Here is my code: import { HttpClient, HttpErrorResponse, HttpHeaders } from \'@angular/common/http\'; logIn(username: string, password: string) { const url = \'http://server.com/index.php\'; const body = JSON.stringify({username: username, password: password}); const headers = new HttpHeaders(); headers.set(\'Content-Type\', \'application/json; charset=utf-8\'); this.http.post(url, body, {headers: headers}).subscribe( (data) => { console.log(data); }, (err: HttpErrorResponse) => { if (err