Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记<

前端之家收集整理的这篇文章主要介绍了Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记<前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从Visual Studio中的Angular2组件服务调用Web API,但我不断收到错误JSON.parse()中位置0的JSON中的意外标记<”. ComponentService:

import { Injectable } from '@angular/core';
       import {Http,Response } from '@angular/http';
       import { IData } from '../Common/details';
       import { Observable } from 'rxjs/Observable';
       import 'rxjs/add/operator/map';

       @Injectable()  
       export class AniversaryService {
         constructor(private _http:Http) { }
         getImages(): Observable<IData[]> {
                return this._http.get("/api/ImageService/Details")
                .map((response: Response) => <IData[]>response.json()      
                };
        }

和相应的组件是:

import { Component,OnInit } from '@angular/core';
    import { DomSanitizer } from '@angular/platform-browser';
    import { IData } from '../Common/details';
    import { AniversaryService } from './Aniversary.service';

    @Component({
    selector: 'my-AniversaryComponent',providers: [AniversaryService]
    })

    export class AniversaryComponent implements OnInit {
       data: IData[];
       constructor(private _aniversaryservice: AniversaryService) { }
       ngOnInit() {
       this._aniversaryservice.getImages().subscribe((details) => this.data 
       =details); 
       }
     }

    }

这些是网络标头和我的响应的图像(响应是在Javascript中):

In Developer tool the network Headers image:

and my response is in Javascript

有时我的状态代码显示200 ok和内容类型(在响应标题中):application / javascript

请帮我解决这个问题.

我在这里先向您的帮助表示感谢

解决方法

标题没用,数据也是如此.但错误信息很明确:您认为应该是JSON,以“<”开头作为第一个字符,JSON不会以“<”开头.很可能你收到的是html或xml.

猜你在找的Angularjs相关文章