2016-05-09 20 views
6

Tôi đã cố tạo một máy chủ với một số dữ liệu giả.Cú pháp mã thông báo lỗi không mong muốn trong JSON ở vị trí 0 Angular2

Đây là System.js Config của tôi (kể từ khi tôi có một định tuyến hơi khác nhau, điều này dường như làm việc tốt cho đến bây giờ)

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

đây là dịch vụ của tôi:

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

tuy nhiên, nếu tôi thay đổi giao diện điều khiển đăng nhập để console.log (dữ liệu) nó trở về không xác định.

Tôi đã xem mọi nơi, nhưng không có câu trả lời nào giải quyết được trường hợp của tôi. Nó có thể là một vấn đề với hệ thống js, nhưng vì mọi thứ khác dường như hoạt động tốt, tôi thực sự không thể tìm cách sửa lỗi này.

Đây là phản ứng trong giao diện điều khiển:

console phản ứng:

enter image description here

+0

phản ứng séc trong tab mạng trình duyệt .. bạn đang nhận được một json sai hay không-một-đúng kết quả như bạn mã hóa cho – Rakeschand

+0

Bạn đã đúng. JSON của tôi không hợp lệ. cảm ơn cho phản ứng mặc dù! –

Trả lời

15

JSON.stringify(undefined) kết quả trong hợp lệ JSON, đây là lý do tại sao JSON.parse(JSON.stringify(data)) ném.

Bạn có thể thay đổi mã của bạn để

JSON.parse(JSON.stringify(data || null)) 
Các vấn đề liên quan