Javascript 位置 0 Angular2 JSON 中的 SyntaxError 意外标记 u

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37114968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:56:40  来源:igfitidea点击:

SyntaxError unexpected token u in JSON on position 0 Angular2

javascriptangularjsjsonangular

提问by Astrid Karsten

I tried to make a server with some dummy data.

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

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

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

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

here is my service:

这是我的服务:

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

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

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

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.

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

This is the response in the console:

这是控制台中的响应:

console response:

控制台响应:

enter image description here

在此处输入图片说明

回答by Günter Z?chbauer

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

JSON.stringify(undefined)导致无效的 JSON,这就是JSON.parse(JSON.stringify(data))抛出的原因。

You might change your code to

您可以将代码更改为

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