简体   繁体   中英

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

I'm having an error in Ionic 2 "SyntaxError: Unexpected token < in JSON at position 3". My json format is correctly structured using spring boot.

Below is my spring boot code.

Appreciate your help.

@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

}

JSON from /myview

[{"id":1,"username":"donald@yahoo.com","policy":"V121293031","name":"Donald","mobile":"0504735260","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":"MetDubai","amount":334.34,"datetimestamp":1472862939000},{"id":4,"username":"dcgatan@yahoo.com","policyno":"V34342323","fname":"Snoopy","mobile":"0501234567","email":"dcgatan@yahoo.com","address":"Metlife Dafza Dubai","amount":883.43,"datetimestamp":1472916463000}]

My http://localhost:8080/myview is not working because when I tried the below code with Array value it works. How to call the http instead of putting static values in the Array?

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);
        }
      }
    }

}

Your call to /myview would be returning incorrect json. It must be having HTML elements. Performing res.json() extracts data from _body of the response, if it's valid. But in your case it is throwing an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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