angular2-http

Angular2: How to subscribe to Http.post observable inside a service and a component properly?

纵然是瞬间 提交于 2021-02-18 22:10:25
问题 For a JWT authentification, I make a post request to get the token using the new Http module working with Observables now. I have a simple Login component showing the form: @Component({ selector: 'my-login', template: `<form (submit)="submitForm($event)"> <input [(ngModel)]="cred.username" type="text" required autofocus> <input [(ngModel)]="cred.password" type="password" required> <button type="submit">Connexion</button> </form>` }) export class LoginComponent { private cred: CredentialsModel

Angular2: How to subscribe to Http.post observable inside a service and a component properly?

本秂侑毒 提交于 2021-02-18 22:10:02
问题 For a JWT authentification, I make a post request to get the token using the new Http module working with Observables now. I have a simple Login component showing the form: @Component({ selector: 'my-login', template: `<form (submit)="submitForm($event)"> <input [(ngModel)]="cred.username" type="text" required autofocus> <input [(ngModel)]="cred.password" type="password" required> <button type="submit">Connexion</button> </form>` }) export class LoginComponent { private cred: CredentialsModel

angular2 - http post request parameters prevent encoding the special characters

烂漫一生 提交于 2021-01-28 09:09:13
问题 i am trying to make a HTTP post request. When using the below code, the request parameters are null in the server side. let url = apiURL; let body = { 'tenantId': tenantId, 'vsLoginToken': vsLoginToken, 'branchId': branchId }; let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); return this._http.post(url, body, options).map(res => res.json()). catch(error => Observable.throw(error.json().error || 'Server error')); If i

cyclic dependency error when importing service into another service

人盡茶涼 提交于 2021-01-28 05:33:04
问题 I'm trying to set default headers for my http requests using the DefaultRequestOptions class provided by Angular 2. Documentation can be found here: https://angular.io/docs/ts/latest/guide/server-communication.html#!#override-default-request-options I want to add a default bearer token, which gets set in one of my services, but doing so gives me the following error in my browser console: Unhandled Promise rejection: Provider parse errors: Cannot instantiate cyclic dependency! Http: in

Angular2: How to send data from client to server when making request

£可爱£侵袭症+ 提交于 2020-05-11 20:03:08
问题 There is form and a button on client side, I want to send data that user typed in the form, to the server, where there is request handler that save data to the database, and from database to the client. How can i do this I'm confused about the logic, I think there is use of body parser, also what is the role of headers, request option in that case, I found that solution but I'm not implementing blindly, I just want to do it my way after understanding At the client side: let headers: Headers =

Angular2 : Chain http requests with concat()

让人想犯罪 __ 提交于 2020-01-15 08:45:08
问题 I have some difficulties to chain several HTTP requests. First, the request is defined in this service method that returns a cold Observable. FormPartService submitFormPart(id: string, part: string, data: any) { let body = JSON.stringify({ claimFileId: claimFileId, part: part, data: data }); return this.http.post('/form-part', body) .map( (res) => { let body = res.json(); if (body.data){ return body.data; } } ); } I have a variable number of form part to submit depending if they are dirty or

Angular 2 http post is returning 200 but no response is returned

风流意气都作罢 提交于 2020-01-15 05:18:06
问题 My http call is returning 200 but no response is captured. My code inside subscribe is not being hit . The API is returning data when I test in postman. Here is my code. getToken(authcode: string) { var data = 'client_id=InspectWebApp_client&code=' + authcode + '&redirect_uri=http://localhost:3000&grant_type=authorization_code'; let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }); let options = new RequestOptions({ headers: headers }); this.http.post('https:/

How to synchronise Angular2 http get?

半世苍凉 提交于 2020-01-11 04:24:26
问题 I understand using observable I can execute a method when the request is completed, but how can i wait till a http get is completed and return the response using in ng2 http? getAllUser(): Array<UserDTO> { this.value = new Array<UserDTO>(); this.http.get("MY_URL") .map(res => res.json()) .subscribe( data => this.value = data, err => console.log(err), () => console.log("Completed") ); return this.value; } the "value" will is null when its returned because get is async.. 回答1: You should not try

How to use angular2 http API for tracking upload/download progress

独自空忆成欢 提交于 2020-01-09 09:01:27
问题 Tho there are many adhoc libraries supporting upload/download progress in angular2, I do not know how to do use native angular2 http api to show progress while doing upload/download. The reason why I want to use native http api is because I want to utilise http interceptors(http API wrappers) around native http api that validate, cache & enrich the actual http request being sent such as this & this Besides angular's http api is much more robust than any adhoc APIs There is this nice article

How to use angular2 http API for tracking upload/download progress

五迷三道 提交于 2020-01-09 09:01:09
问题 Tho there are many adhoc libraries supporting upload/download progress in angular2, I do not know how to do use native angular2 http api to show progress while doing upload/download. The reason why I want to use native http api is because I want to utilise http interceptors(http API wrappers) around native http api that validate, cache & enrich the actual http request being sent such as this & this Besides angular's http api is much more robust than any adhoc APIs There is this nice article