Angular 2 SyntaxError: Unexpected token < in>)

前端 未结 2 1502
长情又很酷
长情又很酷 2021-01-13 07:44

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 JS

相关标签:
2条回答
  • 2021-01-13 08:09

    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.

    0 讨论(0)
  • 2021-01-13 08:21

    -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.

    0 讨论(0)
提交回复
热议问题