Problem solved
I didn't notice I need to add options to the get request. Now it works just fine.
Case can be closed. Thanks
I'm trying to make an http get request to a server I run locally but can't get it working.. been trying to follow many tutorials and yet, nothing works so.. i come to you since i have no idea what is wrong :\
It runs and compiles, but whenever it gets to the point it needs to make the get request it seems like it just skips it and continue, leaving the visitorDetails object undefined.
I'm new to Angular and it's my first app so if i have other problems with the structure or if you have anything to recommend - please do tell me.
I really appreciate the help, thanks in advance!
main.ts:
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { AppModule } from './app/app.module';
platformBrowserDynamic().bootstrapModule(AppModule);
app.module.ts:
import { NgModule } from '@angular/core';
import { BrowserModule }from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppComponent } from './app.component';
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule
],
declarations: [ AppComponent ],
bootstrap: [ AppComponent ]
})
export class AppModule { }
app.component.ts
import { Component, OnInit } from '@angular/core';
import { Http } from '@angular/http';
import { DataService } from './dataservice.service';
import { VisitorDetails } from './visitordetails';
@Component({
selector: 'my-app',
template: `
<h1> {{title}} </h1>
<h2> text: {{visitorsText}} </h2>
`,
providers: [DataService]
})
export class AppComponent implements OnInit {
title = 'TrafficerData';
visitorsText = this.getData();
errorMessage: string;
visitorDetails: VisitorDetails;
constructor (private dataService: DataService) {}
ngOnInit() {
this.getData();
}
getData() : VisitorDetails {
this.dataService.getData()
.subscribe(
val => this.visitorDetails = val,
error => this.errorMessage = <any>error);
return this.visitorDetails;
}
}
app.service.ts:
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { VisitorDetails } from './visitordetails';
@Injectable()
export class DataService {
constructor (private http: Http) {}
getData(): Observable<VisitorDetails> {
return this.http.get('http://localhost:8000/api/client01/visitors')
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError (error: Response | any) {
// In a real world app, you might use a remote logging infrastructure
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
}
}
visitordetails.ts:
export class VisitorDetails {
constructor(
public numOfVisitors: string,
public numOfBlockers: string,
public blockingPercentage: string
){}
}