jeudi 1 juin 2017

Fetch data from json file and show it to DataTable - Angular2

I am new in Angular2 and can't solve next issue.I'll be appreciated for your help. I have some data in data.json file :

[
{
"name": "Jayme", "email": "a.nunc.In@convallisante.ca", "regDate": "2016-02-07T10:22:09-08:00", "city": "Ville de Maniwaki", "age": 30 }, { "name": "Bo", "email": "posuere.cubilia.Curae@estNunclaoreet.net", "regDate": "2016-08-16T20:42:44-07:00", "city": "Pak Pattan", "age": 24 }, { "name": "Matthew", "email": "enim.Mauris.quis@vehicula.edu", "regDate": "2015-05-01T01:53:59-07:00", "city": "Alacant", "age": 35 }, { "name": "Justina", "email": "Donec.nibh@Vivamusmolestie.ca", "regDate": "2015-06-24T14:38:07-07:00", "city": "Kobbegem", "age": 22 } ]

This is my sit_log.component.ts

import { Component,OnInit } from '@angular/core';
import {Http} from "@angular/http";

@Component({
selector: 'sit_log',
templateUrl: './sit_log.html'
})
export class SIT_log implements OnInit{

public data;
public filterQuery = "";
public rowsOnPage = 10;
public sortBy = "email";
public sortOrder = "asc";

constructor(private http: Http) {
}

ngOnInit(): void {
    console.log(window.location.href);

this.http.get("http://localhost:4200/index.html#/pages/sit_log/data.json")
        .subscribe((data)=> {
            setTimeout(()=> {
                this.data = data.json();
            }, 1000);
        });
}

}

When I run the code I receive next error :

core.es5.js:1084 ERROR SyntaxError: Unexpected token < in JSON at position 0
at Object.parse (<anonymous>)
at Response.Body.json (http.es5.js:800)
at SafeSubscriber._next (sit_log.component.ts:23)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:238)
at SafeSubscriber.next (Subscriber.js:185)
at Subscriber._next (Subscriber.js:125)
at Subscriber.next (Subscriber.js:89)
at XMLHttpRequest.onLoad (http.es5.js:1229)
at ZoneDelegate.webpackJsonp.688.ZoneDelegate.invokeTask (zone.js:424)
at Object.onInvokeTask (core.es5.js:4140)

Have anybody some idea? Regards.




Aucun commentaire:

Enregistrer un commentaire