Angular 2 SyntaxError:意外令牌<在JSON.parse(< anonymous>)位置0的JSON中
我正在从Visual Studio的Angular2组件服务中调用Web API,但是不断出现错误"JSON.parse()中位置0的JSON中的意外令牌<".
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:
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()
};
}
,对应的组件是:
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);
}
}
}
这些是网络标头和我的响应的图像(响应是Java语言):
These are the images of network Headers and my response (response is in Javascript):
有时我的状态码显示200 ok和内容类型(在响应标题中):application/javascript
Sometimes my status code showing 200 ok and content type (in Response Headers): application/javascript
请帮助我解决这个问题.
Please help me to solve this problem.
感谢您的提前帮助
标头没有用,数据有用.但是错误消息很明确:您认为应该是JSON的内容,以<"开头作为第一个字符,JSON永远不会以<"开头.您很可能会收到html或xml.
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.