【问题标题】:ERROR SyntaxError: Unexpected token : in JSON at position 6 in angular 2错误语法错误:意外标记:在 JSON 中,角度 2 中的位置 6
【发布时间】:2017-04-15 14:23:25
【问题描述】:

我在 Angular 2 应用程序中集成了 Angular 完整日历,但是当我尝试从包含事件列表的 json 文件中获取数据时,在 console.log 中出现错误。 错误:

ERROR SyntaxError: Unexpected token : in JSON at position 6
    at Object.parse (<anonymous>)
    at Response.Body.json (http.es5.js:796)
    at MapSubscriber.project (events.service.ts:15)
    at MapSubscriber._next (map.js:77)
    at MapSubscriber.Subscriber.next (Subscriber.js:89)
    at XMLHttpRequest.onLoad (http.es5.js:1205)
    at ZoneDelegate.webpackJsonp.706.ZoneDelegate.invokeTask (zone.js:398)
    at Object.onInvokeTask (core.es5.js:4116)
    at ZoneDelegate.webpackJsonp.706.ZoneDelegate.invokeTask (zone.js:397)
    at Zone.webpackJsonp.706.Zone.runTask (zone.js:165)
    at XMLHttpRequest.ZoneTask.invoke (zone.js:460)

full-calendar.component.ts

 export class FullCalendarComponent implements OnInit {

  getEvent:object;

  constructor(private eventsService:EventsService){}

  ngOnInit(){

      this.eventsService.getEvents().subscribe(getEvent => this.getEvent = getEvent);
  }
}

events.service.ts

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class EventsService {

  constructor(private http: Http) { }
  getEvents()
  {

    return this.http.get('src/assets/data/events.json').map(response => response.json().data);

  }
}

events.json

{
    "date": [
          {
            "title": "All Day Event",
            "start": "2017-04-01"
          }
        ]
}

【问题讨论】:

    标签: angular


    【解决方案1】:

    查看您正在使用data 属性的服务中的方法,您的响应返回date

      getEvents()
      {
    
        return this.http
                       .get('src/assets/data/events.json')
                       .map(response => response.json().date);////////////////////////////
    
      }
    

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 2023-03-09
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      相关资源
      最近更新 更多