【问题标题】:Angular 2 SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>)Angular 2 SyntaxError:JSON.parse (<anonymous>) 位置 0 处的 JSON 中的意外标记 <
【发布时间】:2018-04-21 18:49:30
【问题描述】:

我在 Visual Studio 中从我的 Angular2 组件服务调用 Web API,但我不断收到错误“Unexpected token

组件服务:

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

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

请帮我解决这个问题。

提前感谢您的帮助

【问题讨论】:

    标签: angular typescript asp.net-web-api


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      标题没有用,数据有用。但错误信息很明确:您认为应该是 JSON,以“

      【讨论】:

      • 感谢您的回复。为了确保 Web API 的输出是 JSON,我添加了“config.Formatters.Clear();”、“config.Formatters.Add(new JsonMediaTypeFormatter());”。不管是什么,我都会重新检查我的整个应用程序,并确保它是你提到的 JSON。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多