deen
deen

Reputation: 2305

Server Data is not Coming: Angular2

I have created a simple app using REST in java which return string value successfully with REST client. Now I want to get string value using Http rest client in Angular2. I have created service for getting data from rest client in angular2 which saying successfully access rest but when I am printing data like {{serverData}} it's print nothing.

service.ts

@Injectable()
export class HttpSiftgridService {
private url:string = "http://localhost:8080/app-rest/rest  /get/getData";
constructor(private _http: Http) {}
getSiftgridData() {
    alert(this._http.get(this.url).map(res => res.json));
    alert("hh");
    return this._http.get(this.url).map(res => res.json);
}
private handleError(error : Response) {
    console.error(error);
    return Observable.throw(error.json().error || ' error');
}

}

app.component.ts

export class AppComponent implements OnInit{ 
serverData: string;
constructor(private _httpService:HttpSiftgridService) {}
ngOnInit() {
    this._httpService.getSiftgridData()
        .subscribe(
          data => this.serverData = JSON.stringify(data),
          error => console.log("Error in getting Data"),
          () => console.log("Successfully")
    );
}

}

my rest app running on tomcat.

Upvotes: 1

Views: 72

Answers (2)

stijn.aerts
stijn.aerts

Reputation: 6206

Change:

return this._http.get(this.url).map(res => res.json);

To:

return this._http.get(this.url).map(res => res.json());

Upvotes: 1

Günter Zöchbauer
Günter Zöchbauer

Reputation: 657248

I know it's not a real answer but that wouldn't work as comment:

If you change it to

ngOnInit() {
    this._httpService.getSiftgridData()
        .subscribe(
          data => {
            this.serverData = JSON.stringify(data);
            console.log(data);
          },
          error => console.log("Error in getting Data"),
          () => console.log("Successfully")
    );
}

is the data printed?
Is Successfully printed?

Upvotes: 0

Related Questions