Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

In Developer tool the network Headers image:

and my 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

like image 625
Bhargav Avatar asked Nov 08 '22 14:11

Bhargav


1 Answers

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.

like image 166
gnasher729 Avatar answered Nov 14 '22 21:11

gnasher729