angular2-http

Angular 2 Http, Observables and recursive requests

好久不见. 提交于 2019-11-27 14:49:46
I have a REST endpoint that returns a list of items, max 1000 items at a time. If there are more than 1000 items, the response has HTTP status 206 and there's a Next-Range header that I can use in my next request for getting more items. I'm working on an Angular 2 application and trying to implement this with Http and Observable . My problem is that I don't know how to merge multiple Observable s depending on how many pages of items there are and finally return one Observable that my component can subscribe to. Here's where I've got with my current TypeScript implementation: // NOTE: Non

angular 2 http withCredentials

不羁岁月 提交于 2019-11-27 11:28:59
问题 I'm am trying to use withCredentials to send a cookie along to my service but can't find out how to implement it. The docs say "If the server requires user credentials, we'll enable them in the request headers" With no examples. I have tried several different ways but it still will not send my cookie. Here is my code so far. private systemConnect(token) { let headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('X-CSRF-Token', token.token); let options =

Angular: Typescript casting JSON response as object model not working

十年热恋 提交于 2019-11-27 09:38:57
I have an issue while I try to cast a json response to object, all the properties of my object are string is that normal ? Here is my ajax request : public getSingle = (keys: any[]): Observable<Badge> => { return this._http.get(this.actionUrl + this.getKeysUrl(keys)) .map((response: Response) => response.json() as Badge ) .catch(this.handleError); } Here is my badge model : export interface Badge { badgeNumber: number; authorizationLevel: number; endOfValidity: Date; } And here is where I call the service function and I'm facing the issue : this._badgeService.getSingle(this.ids).subscribe(

Set base url for angular 2 http requests

混江龙づ霸主 提交于 2019-11-27 06:52:43
I am trying to set base url for all my angular 2 http requests. Following is the basic set up for my application. class HttpOptions extends BaseRequestOptions { url:string = "http://10.7.18.21:8080/api/"; } bootstrap(AppComponent, [ HTTP_PROVIDERS, provide(RequestOptions, {useClass: HttpOptions}) ]); export class AppComponent { users:Array<User> constructor(private http: Http) { http.get("/users") .subscribe(res => this.users = res.json()); } } The request is not sent to http://10.7.18.21:8080/api/users as I expected from my configuration. Instead the request is sent to http://localhost:8000

angular 2 - Injected service in http error handler

冷暖自知 提交于 2019-11-27 06:14:49
问题 I have a method handleError() like in the documentation https://angular.io/docs/ts/latest/guide/server-communication.html#!#error-handling private handleError(error: any) { console.error(error); console.log(this.loginService); // <- always undefined return Observable.throw(error); } My problem is, that this.loginService is undefined although it has been injected in my class correctly. It is already used in other methods but seems not to be available in handleError . Could the way the method

How to correctly set Http Request Header in Angular 2

佐手、 提交于 2019-11-26 22:43:25
I have an Ionic 2 application using Angular 2, which is sending an Http PUT to a ASP.NET Core API server. Here's the method I'm using to send the request: public update(student: Student): Promise<Student> { let headers = new Headers(); headers.append('Content-Type', 'application/json'); headers.append('authentication', `${student.token}`); const url = `${this.studentsUrl}`; return this.http .put(url, JSON.stringify(student), { headers: headers }) .toPromise() .then(() => student) .catch(this.handleError); } I'm setting an authentication key/value on the headers object. But when I receive this

How to use/import http module?

爱⌒轻易说出口 提交于 2019-11-26 20:19:59
I've been playing with Angular 2 Quickstart . How can I use/import http module in Angular 2? I've looked at Angular 2 Todo's.js , but it doesn't use the http module. I've added "ngHttp": "angular/http", to dependencies in package.json because I've heard Angular 2 is somewhat modular. In version 37 you need to do this: ///<reference path="typings/angular2/http.d.ts"/> import {Http} from "angular2/http"; And run this tsd command: tsd install angular2/http Last update: May 11, 2016 Angular version: 2.0.0-rc.2 Typescript version: 1.8.10 Live working example . A simple example of how to use the

Get Image or byte data with http

送分小仙女□ 提交于 2019-11-26 19:56:23
问题 For a web application I need to get my images with an ajax request because we have signature + authentication on our API, so we can't get images using a simple <img src="myapi/example/145"/> Since we're using angular2, we obviously looked for blob or something like that, but as stated in static_response.d.ts file: /** * Not yet implemented */ blob(): any; So okay, I can't do it for now, I have to wait for thie feature to be implemented. But problem is I can't wait so I need a hotfix or a

File Upload with Angular2 to REST API

巧了我就是萌 提交于 2019-11-26 19:29:35
Actually, I'm working on a Spring REST API with an interface coded in Angular 2. My problem is I can't upload a file with Angular 2. My Webresources in java is that : @RequestMapping(method = RequestMethod.POST, value = "/upload") public String handleFileUpload(@RequestParam MultipartFile file) { //Dosomething } And it is perfectly working when I call it through URL request with Auth header etc ... ( with Advanced Rest Client extension for Chrome ) Proof: (everything works fine in that case ) I added the <bean id="multipartResolver" class="org.springframework.web.multipart.commons

Angular 2 - Routing - CanActivate work with Observable

为君一笑 提交于 2019-11-26 18:49:34
问题 I have an AuthGuard (used for routing) that implements CanActivate . canActivate() { return this.loginService.isLoggedIn(); } My problem is, that the CanActivate-result depends on a http-get-result - the LoginService returns an Observable . isLoggedIn():Observable<boolean> { return this.http.get(ApiResources.LOGON).map(response => response.ok); } How can i bring those together - make CanActivate depend on a backend state? 回答1: You should upgrade "@angular/router" to the latest . e.g."3.0.0