加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular 2 SyntaxError:JSON.parse()中位置0的JSON中的意外标记

发布时间:2020-12-17 18:10:54 所属栏目:安全 来源:网络整理
导读:我从Visual Studio中的Angular2组件服务调用Web API,但我不断收到错误“ JSON.parse()中位置0的JSON中的意外标记”. ComponentService: import { Injectable } from '@angular/core'; import {Http,Response } from '@angular/http'; import { IData } from
我从Visual Studio中的Angular2组件服务调用Web API,但我不断收到错误“ JSON.parse()中位置0的JSON中的意外标记<”. 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); 
       }
     }

    }

这些是网络标头和我的响应的图像(响应是在Javascript中):

In Developer tool the network Headers image:

and my response is in Javascript

有时我的状态代码显示200 ok和内容类型(在响应标题中):application / javascript

请帮我解决这个问题.

我在这里先向您的帮助表示感谢

解决方法

标题没用,数据也是如此.但错误信息很明确:您认为应该是JSON,以“<”开头作为第一个字符,JSON不会以“<”开头.很可能你收到的是html或xml.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读