位于0位置Angular2的JSON中的SyntaxError意外标记u [英] SyntaxError unexpected token u in JSON on position 0 Angular2

查看:119
本文介绍了位于0位置Angular2的JSON中的SyntaxError意外标记u的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一些虚拟数据制作服务器。

I tried to make a server with some dummy data.

这是我的System.js配置(因为我有一个稍微不同的路由,这似乎工作正常到目前为止)

Here is my System.js Config (since I have a slightly different routing, this seemed to work fine up till now)

System.config({

            // baseURL to node_modules
            baseURL: '/plugins/dashboard/assets/@@version@@/node_modules',
            defaultJSExtensions: true
        });
        System.import('/plugins/dashboard/assets/@@version@@/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/@@version@@/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');
  }
}

但是,如果我将控制台日志更改为控制台。 log(data)它返回undefined。

however, if I change the console log to console.log(data) it is returning undefined.

我到处寻找,但没有一个答案解决了我的情况。这可能是系统js的一个问题,但由于其他一切似乎都运行正常,我真的找不到如何解决这个问题。

I looked everywhere, but none of the answers solved my case. It might be a problem with system js, but since everything else seems to work fine, I really cannot find how to fix this.

这是控制台中的响应:

This is the response in the console:

控制台响应:

推荐答案

JSON.stringify(未定义)导致JSON无效,这就是为什么 JSON.parse(JSON.stringify(data)) throws。

JSON.stringify(undefined) results in invalid JSON, this is why JSON.parse(JSON.stringify(data)) throws.

您可能将代码更改为

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

这篇关于位于0位置Angular2的JSON中的SyntaxError意外标记u的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆