2016-05-09 15 views
6

Ho provato a creare un server con alcuni dati fittizi.SyntaxError token imprevisto u in JSON sulla posizione 0 Angular2

Qui è il mio System.js Config (dal momento che ho un percorso leggermente diverso, questo sembrava funzionare bene fino ad ora)

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

qui è il mio servizio:

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

tuttavia, se cambio il log della console in console.log (dati) restituisce undefined.

Ho cercato ovunque, ma nessuna delle risposte ha risolto il mio caso. Potrebbe essere un problema con il sistema js, ma dal momento che tutto il resto sembra funzionare bene, non riesco davvero a trovare come risolvere questo problema.

Questa è la risposta nella console: risposta

console:

enter image description here

+0

risposta check-in scheda di rete del browser .. si sta rilevando un JSON sbagliato o non-a-corretto risultato durante la codificato per – Rakeschand

+0

Avevi ragione. Il mio JSON non era valido. Comunque grazie per la risposta! –

risposta

15

JSON.stringify(undefined) risultati in invalida JSON, questo è il motivo per cui JSON.parse(JSON.stringify(data)) tiri liberi.

si potrebbe modificare il codice per

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