Angular 2 SyntaxError:意外令牌<在JSON.parse(< anonymous>)位置0的JSON中 [英] Angular 2 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)

查看:80
本文介绍了Angular 2 SyntaxError:意外令牌<在JSON.parse(< anonymous>)位置0的JSON中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Visual Studio的Angular2组件服务中调用Web API,但是不断出现错误"JSON.parse()中位置0的JSON中的意外令牌<".

I am calling the web API from my Angular2 Component service in Visual Studio, but continuously I am getting the error "Unexpected token < in JSON at position 0 at JSON.parse ()".

ComponentService:

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

    }

这些是网络标头和我的响应的图像(响应是Java语言):

These are the images of network Headers and my response (response is in Javascript):

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

Sometimes my status code showing 200 ok and content type (in Response Headers): application/javascript

请帮助我解决这个问题.

Please help me to solve this problem.

感谢您的提前帮助

推荐答案

标头没有用,数据有用.但是错误消息很明确:您认为应该是JSON的内容,以<"开头作为第一个字符,JSON永远不会以<"开头.您很可能会收到html或xml.

The headers are not useful, the data would be. But the error message is clear: What you think is supposed to be JSON, starts with a "<" as the first character, and JSON doesn't ever start with a "<". Most likely you are receiving either html or xml.

这篇关于Angular 2 SyntaxError:意外令牌&lt;在JSON.parse(&lt; anonymous&gt;)位置0的JSON中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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