2017-06-01 37 views
0

私はAngular2で新しく、次の問題を解決することはできません。ご協力いただければ幸いです。jsonファイルからデータを取得し、DataTableに表示する - Angular2

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) 

:私は次のエラーメッセージが表示さ

[ 
{ 
    "name": "Jayme", 
    "email": "[email protected]", 
    "regDate": "2016-02-07T10:22:09-08:00", 
    "city": "Ville de Maniwaki", 
    "age": 30 
    }, 
    { 
    "name": "Bo", 
    "email": "[email protected]", 
    "regDate": "2016-08-16T20:42:44-07:00", 
    "city": "Pak Pattan", 
    "age": 24 
    }, 
    { 
    "name": "Matthew", 
    "email": "[email protected]", 
    "regDate": "2015-05-01T01:53:59-07:00", 
    "city": "Alacant", 
    "age": 35 
    }, 
    { 
    "name": "Justina", 
    "email": "[email protected]", 
    "regDate": "2015-06-24T14:38:07-07:00", 
    "city": "Kobbegem", 
    "age": 22 
    } 
] 

これは私の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); 
     }); 
    } 

} 

私は、コードを実行

さ: 私はいくつかのdata.jsonファイル内のデータを持っています誰かに何か考えてもらえますか?よろしくです。 よろしくお願いします。

+0

は '私はアプリ –

+0

@OluwafemiSule 'のコンテンツの前にスペースをトリムJSON形式のデータを取得していると思いますが、JSON形式に解析しています –

+0

@evgenyに誤りが'にconsole.log(データ)を使用してみてください。この原因を考えていないdata.json' –

答えて

0

ありがとうございました:) 私はdata.jsonファイルをassets/dataディレクティブに置き換えました。すべて正常に動作します。 おかげで再び:)

関連する問題