Angular 2 SyntaxError: Unexpected token < in>)

2019-04-29 23:29发布

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

2条回答
一夜七次
2楼-- · 2019-04-30 00:04

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.

查看更多
▲ chillily
3楼-- · 2019-04-30 00:11

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

查看更多
登录 后发表回答