Angular Response error : SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse
Angular Response error : SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse
我想从 API 获取我的数据。 API 本身在 postmap 中工作正常,如下所示:
{
"status": "Success",
"data": [
{
"title": "asus g551 vw",
"price": 500,
"image": "https://localhost:44345/images/products/origin/img1.jpg",
"count": 2
},
{
"title": "asus vivobook",
"price": 1200,
"image": "https://localhost:44345/images/products/origin/img1.jpg",
"count": 2
},
{
"title": "acer predator",
"price": 200,
"image": "https://localhost:44345/images/products/origin/img1.jpg",
"count": 1
}
]
}
但是在 Angular 中,当我调用我的 get 函数时,我从这个 Angular 代码中得到一个错误:
myservice method 'GetUserBasketDetails()' :
@Injectable({
providedIn: 'root'
})
export class OrderService {
private orderDetails : BehaviorSubject<OrderBasketDto[]> = new BehaviorSubject<OrderBasketDto[]>(null);
constructor(private _http : HttpClient) { }
_SetOrderDetails(details : OrderBasketDto[]){
this.orderDetails.next(details);
console.log(this.orderDetails)
}
_GetOrderDetails() : Observable<OrderBasketDto[]>{
return this.orderDetails;
}
GetUserBasketDetails():Observable<IResponseResult<OrderBasketDto[]>>{
return this._http.get<IResponseResult<OrderBasketDto[]>>('/basket-details');
}
}
当我在组件中调用它时:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'Front-Project';
constructor(private _accountService : AccountService,
private _orderService : OrderService) {
}
ngOnInit():void {
this._orderService.GetUserBasketDetails().subscribe(res=>{
console.log(res)
})
}
}
这是我得到的错误:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://localhost:44345/basket-details", ok: false, …}
和
Error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:20175:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:10720:31)
in console
console error image
我该如何解决这个问题?
我发现我的 url 的问题是错误的 我忘记在 get 方法中添加 api 控制器名称
像这样'/order/basket-details'
我想从 API 获取我的数据。 API 本身在 postmap 中工作正常,如下所示:
{
"status": "Success",
"data": [
{
"title": "asus g551 vw",
"price": 500,
"image": "https://localhost:44345/images/products/origin/img1.jpg",
"count": 2
},
{
"title": "asus vivobook",
"price": 1200,
"image": "https://localhost:44345/images/products/origin/img1.jpg",
"count": 2
},
{
"title": "acer predator",
"price": 200,
"image": "https://localhost:44345/images/products/origin/img1.jpg",
"count": 1
}
]
}
但是在 Angular 中,当我调用我的 get 函数时,我从这个 Angular 代码中得到一个错误:
myservice method 'GetUserBasketDetails()' :
@Injectable({
providedIn: 'root'
})
export class OrderService {
private orderDetails : BehaviorSubject<OrderBasketDto[]> = new BehaviorSubject<OrderBasketDto[]>(null);
constructor(private _http : HttpClient) { }
_SetOrderDetails(details : OrderBasketDto[]){
this.orderDetails.next(details);
console.log(this.orderDetails)
}
_GetOrderDetails() : Observable<OrderBasketDto[]>{
return this.orderDetails;
}
GetUserBasketDetails():Observable<IResponseResult<OrderBasketDto[]>>{
return this._http.get<IResponseResult<OrderBasketDto[]>>('/basket-details');
}
}
当我在组件中调用它时:
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit{
title = 'Front-Project';
constructor(private _accountService : AccountService,
private _orderService : OrderService) {
}
ngOnInit():void {
this._orderService.GetUserBasketDetails().subscribe(res=>{
console.log(res)
})
}
}
这是我得到的错误:
HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "https://localhost:44345/basket-details", ok: false, …}
和
Error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at XMLHttpRequest.onLoad (http://localhost:4200/vendor.js:20175:51) at ZoneDelegate.invokeTask (http://localhost:4200/polyfills.js:10720:31) in console
console error image
我该如何解决这个问题?
我发现我的 url 的问题是错误的 我忘记在 get 方法中添加 api 控制器名称 像这样'/order/basket-details'