繁体   English   中英

SyntaxError:意外的令牌<在ionic 2中位置3的JSON中<

[英]SyntaxError: Unexpected token < in JSON at position 3 in ionic 2

我在Ionic 2中出现错误“ SyntaxError:位置3的JSON中的意外令牌<”。 我的json格式是使用Spring Boot正确构造的。

以下是我的春季启动代码。

感谢您的帮助。

@RequestMapping(value="/myview", method=RequestMethod.GET, produces = "application/json")
    @ResponseBody
    List<Client> myView( @ModelAttribute("client") Client client){


        List<Client> data=(List<Client>) clientService.getAll();


        return data;
    }

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class PeopleService {
  people: any;

  constructor(public http: Http) {}

load(){

    if (this.people) {

  return Promise.resolve(this.people);

  }

 return new Promise(resolve => {
    this.http.get('http://localhost:8080/myview')
    .map((res)=>res.json()).subscribe((data)=>{
       console.log(data);
       this.people=data;
       resolve(this.people);
     }, err=>{console.log(err)});
  });
}// end load function

}

来自/ myview的JSON

[{“ id”:1,“ username”:“ donald@yahoo.com”,“ policy”:“ V121293031”,“ name”:“ Donald”,“ mobile”:“ 0504735260”,“ email”:“ dcgatan @ gmail.com”,“地址”:“ Dafza Dubai”,“金额”:800.98,“ datetimestamp”:1472861297000},{“ id”:3,“用户名”:“ dcgatan78@gmail.com”,“ policyno” :“ V38998933”,“ fname”:“ Donald”,“移动”:“ 0501234567”,“电子邮件”:“ dcgatan@gmail.com”,“地址”:“ MetDubai”,“金额”:334.34,“ datetimestamp” :1472862939000},{“ id”:4,“用户名”:“ dcgatan@yahoo.com”,“ policyno”:“ V34342323”,“ fname”:“史努比”,“移动”:“ 0501234567”,“电子邮件” :“ dcgatan@yahoo.com”,“地址”:“迪拜大都会饭店”,“金额”:883.43,“ datetimestamp”:1472916463000}]

我的http:// localhost:8080 / myview无法正常工作,因为当我尝试以下带有Array值的代码时,它可以工作。 如何调用http而不是在数组中放置静态值?

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';


@Injectable()
export class PeopleService {

  people: Array<any> = [{"id":1,"username":"donald@yahoo.com","policyno":"V121293031","fname":"Donald","mobile":"0504735250","email":"dcgatan@gmail.com","address":"Dafza Dubai","amount":800.98,"datetimestamp":1472861297000},{"id":3,"username":"dcgatan78@gmail.com","policyno":"V38998933","fname":"Donald","mobile":"0501234567","email":"dcgatan@gmail.com","address":"MetLife Dubai","amount":334.34,"datetimestamp":1472862939000}];


  constructor(private http: Http) {}

load(){

    if (this.people) {

  return Promise.resolve(this.people);

  }

 return new Promise(resolve => {

    this.http.get('http://localhost:8080/myview')
    .map((res)=>res.json())
    .subscribe((data)=>{
       this.setPeople(data);
       resolve(this.people);
     });
  });
}// end load function

setPeople(data) {
      if (data) {
        for (let id of Object.keys(data)) {
          let item = data[id];

          item.id = id;

          this.people.push(item);
        }
      }
    }

}

您对/ myview的调用将返回错误的json。 它必须具有HTML元素。 执行res.json()会从响应的_body中提取数据(如果有效)。 但是在您的情况下,它将引发错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM