2016-05-09 19 views
6

Bazı sahte veriler içeren bir sunucu yapmaya çalıştım.SözdizimiOnum beklenmedik jetonu JSON konumunda 0 Angular2

İşte benim System.js Yapılandırma (Ben biraz daha farklı bir yönlendirme olmadığından, bu şu ana kadar iyi iş gibi görünüyordu) burada

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)); 

benim hizmettir: Ancak

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'); 
    } 
} 

, konsol günlüğünü console.log (data) olarak değiştirirseniz, undefined döner.

Her yere baktım ama cevapların hiçbiri durumumu çözmedi. Sistem js ile ilgili bir sorun olabilir, ancak her şey iyi çalışıyor gibi göründüğü için, bunu nasıl düzelteceğimi gerçekten bulamıyorum.

konsol cevabı:: geçersiz JSON

enter image description here

+0

onay yanıtı için kodunuzu değişebilir .. sen haklıydın – Rakeschand

+0

için kodlanmış olarak bir yanlış json veya olmayanHoward uygun bir sonuç alıyorsanız. JSON'um geçerli değil. Yine de cevabınız için teşekkürler! –

cevap

15

JSON.stringify(undefined) sonuçları JSON.parse(JSON.stringify(data)) atar bu yüzden

Bu

konsolunda yanıttır.

Tarayıcı ağ sekmesinde

JSON.parse(JSON.stringify(data || null))