typescript Angular 2 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47190620/
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-10-21 05:01:23  来源:igfitidea点击:

Angular 2 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)

angulartypescriptasp.net-web-api

提问by Bhargav

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 ()".

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

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

and corresponding component is:

和相应的组件是:

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

    }

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

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

In Developer tool the network Headers image:

在开发者工具中,网络标题图像:

and my response is in Javascript

我的回复是用 Javascript

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

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

Please help me to solve this problem.

请帮我解决这个问题。

Thanks for the help in advance

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

回答by gnasher729

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.

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

回答by Bhargav

-I recreated my component service

- 我重新创建了我的组件服务

import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/throw';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/do';
import { IDetails } from '../share/Details';

    @Injectable()
    export class AniversaryService {
        private url = 'http://localhost:60975/api/ImageService';
        constructor(private _http: HttpClient) { }
        getDetails(): Observable<IDetails[]> {
            return this._http.get<IDetails[]>(this.url)
                .do(data => console.log(JSON.stringify(data)))
                .catch(this.handleError);
        }
        private handleError(err: HttpErrorResponse) {
            console.log(err.message);
            return Observable.throw(err.message);
        }
    }

now it's accepting the data without any error.

现在它正在接受数据而没有任何错误。