簡體   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