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:
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):
Sometimes my status code showing 200 ok and content type (in Response Headers): application/javascript
Please help me to solve this problem.
Thanks for the help in advance
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With