angular2-http

Is it necessary to unsubscribe from observables created by Http methods?

淺唱寂寞╮ 提交于 2019-12-16 19:53:24
问题 Do you need to unsubscribe from Angular 2 http calls to prevent memory leak? fetchFilm(index) { var sub = this._http.get(`http://example.com`) .map(result => result.json()) .map(json => { dispatch(this.receiveFilm(json)); }) .subscribe(e=>sub.unsubscribe()); ... 回答1: So the answer is no, you don't. Ng2 will clean it up itself. The Http service source, from Angular's Http XHR backend source: Notice how it runs the complete() after getting the result. This means it actually unsubscribes on

Error handling in Angular2

前提是你 提交于 2019-12-13 07:17:52
问题 I'm working on an Angular 2 service returning data from a restful WebApi backend. I'm trying to make it gracefully handle the service not being available however I'm getting unexpected information in the error responses. Here's the code update(fund, transactionId:string, showToastOnError: boolean, useCorrectUrl: boolean) { let options = this.getStartCall(); options.headers.append("transaction-id", transactionId) let url = fundsUrl + (useCorrectUrl ? "" : "breakTheUrl"); return this._http.put

Angular 2 & RxJs catch function callback binding to 'this' causes http request to be repeated over and over

拈花ヽ惹草 提交于 2019-12-12 19:53:04
问题 I have a method for handle our errors from http requests and it looks like this public handleError(err: any, caught: Observable<any>): Observable<any> { //irrelevant code removed this.logger.debug(err);//example of problem return caught; } It is invoked like this (example method, but shows the error) public makeHttpCall() { this.http.get("http://api.exmaple.com/getsomedata") .map(r=> r.json()) .catch(this.handleError); } The problem with the above code is that when calling this.logger.debug

Call PHP file in Angular2

ぐ巨炮叔叔 提交于 2019-12-12 12:25:02
问题 I am new to Angular2. I have one PHP file and would like to fetch content from PHP file in Angular component. Following is my code I have written by taking references. I am not able to figure out what is the issue as I am not getting any error in console nor desired output. PHP code - 'getArea.php' <?php $link = mysql_connect('localhost', 'root', ''); mysql_select_db("mydb", $link); $data=array(); $result = mysql_query("SELECT * FROM dam_color", $link); $num_rows = mysql_num_rows($result);

Can't use concatMap() with angular2 Http

耗尽温柔 提交于 2019-12-12 05:12:36
问题 I need to chain many http requests, the number of request is variable and they are not dependant from the result of the previous one, I just need to keep the returned object of the last request. I've been given two solutions on this thread, the first solution using Observable.concat() works, but a contributor suggested me a more elegant way to perform that, by using concatMap() . Here's my current code : MyComponent submitForms() { var formToSubmit = [...]; // Contains dirty forms that be

Angular 2 - Http request method which is called on both success and error cases

时间秒杀一切 提交于 2019-12-12 02:46:39
问题 All my Backend API requests return new token information in headers, even when them throw exceptions. In the next request I need to send these new token information. So I'm trying to figure out an unique and standard way to do that: let requestOptions = new RequestOptions(Object.assign({ method: method, url: environment.apiHost + url, body: body, headers: authenticatedRequest ? this.requestService.getAuthHeaders() : this.requestService.getJsonHeaders(), withCredentials: authenticatedRequest }

Error SEC7120: [CORS] The origin 'http://localhost:4200' did not find 'http://localhost:4200' in the Access-Control-Allow-Origin response

不羁岁月 提交于 2019-12-11 20:55:16
问题 Actually I am trying to connect an Angular app with my Maven-Jersey-webapp(implements Rest Services concept) and getting this error. More Details: I am using Oracle10g XE Database Eclipse IDE for maven Project(http://localhost:8080/MavenProject) Angular CLI(http://localhost:4200/) I want to retrieve data from database(Stored in students table) and then display that data in Angular App in a Table my jersey project returns the table data in JSON and XML format(Running Successfully) retrieving

Can't have a timeout of over 2 minutes with this.http.get?

点点圈 提交于 2019-12-11 17:15:43
问题 My angular 4.3.2 code is calling my back-end service that takes 2-4 minutes to return. Using just the default this.http.get code, I see that the default timeout kicks in after 2 minutes. However when I try to put in a timeout of anything OVER 2 minutes, it fails in that it will never let the timeout be over 2 minutes. I've tried with 100, 100000 (1.7m) and 114000(1.9m) and those work in that it gets timed out right at those values. But when I try 126000 (2.1m), 180000 (3m) and 1800000 (30m),

Angular2 : Reduce number of Http calls

独自空忆成欢 提交于 2019-12-11 06:37:32
问题 I'm using Angular2 RC5 with an ASP.NET Core server that makes the API calls to get my data. I'm actually wondering if there is a way to reduce the number of http calls you make with Angular2, because I fear there will be a lot if I keep using components the way I do. Here is a concrete example. I want to get a text value from the database, which is defined by an ID and a Language. I then made the following component : dico.component.ts @Component({ selector: 'dico', template: `{{text}}`,

How to preserve object property casing when using Rxjs map function

白昼怎懂夜的黑 提交于 2019-12-11 01:52:06
问题 I am new to Angular2 and using http service to get data from asp.net mvc webapi. API returns data which is in format shown below RequestResponse{ public string Message {get;set;} public int Status {get;set;} } Http get request gets data and is fed to map function from Rxjs map(result => result.json()) which will convert result into format as shown below { message : 'success!', status : 1 } As you can see, output property names have lost case sensitivity. Have read from other SO questions that