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