2016-05-09 4 views
6

ダミーデータでサーバーを作成しようとしました。JSONのSyntaxError予期しないトークンuが0の位置にあります。Angular2

はここに私のSystem.js configです(私は少し異なるルーティングを持っているので、これは今まで正常に動作するように見えた)ここ

System.config({ 

      // baseURL to node_modules 
      baseURL: '/plugins/dashboard/assets/@@[email protected]@/node_modules', 
      defaultJSExtensions: true 
     }); 
     System.import('/plugins/dashboard/assets/@@[email protected]@/app/main') 
       .then(null, console.error.bind(console)); 

は私のサービスです。しかし

import {Injectable}  from 'angular2/core'; 
import {Http, Response} from 'angular2/http'; 
//import {Observable }  from 'rxjs/Observable'; 
import {Observable} from 'rxjs/Rx'; 

import {newsLetter} from "../objects/newsLetter"; 


@Injectable() 

export class newsLetterService { 
constructor (private http: Http) {} 

//Need to change this URL 
private _myNewsLetterUrl = "http://epub-core.dev.prisma-it.com:8888/plugins/dashboard/assets/@@[email protected]@/app/data/newsLetter.json"; // URL to web api 

getNewsLetter() { 
    console.log(this._myNewsLetterUrl); 
    console.log(this.http.get(this._myNewsLetterUrl)); 
    return this.http.get(this._myNewsLetterUrl) 
     .map(res => <newsLetter[]> res.json().data) 
     // eyeball objects as json objects in the console | mapping purposes 
     .do(data => console.log(JSON.parse(JSON.stringify(data)))) 
      .catch(this.handleError); 

    } 

    private handleError (error: Response) { 
    // in a real world app, we may send the error to some remote logging infrastructure 
    // instead of just logging it to the console 
    console.error(error); 
    return Observable.throw(error.json().error || 'Server error'); 
    } 
} 

、コンソールログをconsole.log(data)に変更すると、undefinedが返されます。

私はどこにでも見えましたが、答えのどれも私の場合を解決しませんでした。それはシステムjの問題かもしれませんが、他のすべてが正常に動作しているようだから、これを修正する方法は本当に見つけられません。

これは、コンソールでの応答である:

コンソールの応答:無効なJSONで

enter image description here

+0

チェック応答するようにコードを変更する場合があります..あなたは、あなたがあなたは正しかった – Rakeschand

+0

ためにコード化されたとして、間違ってJSONか-適切な結果を得ています。私のJSONは有効ではありませんでした。しかし、応答のためにありがとう! –

答えて

15

JSON.stringify(undefined)結果JSON.parse(JSON.stringify(data))スローなぜ、これがあります。

あなたは、ブラウザのネットワークタブで

JSON.parse(JSON.stringify(data || null)) 
関連する問題