SyntaxError: Unexpected token < in JSON at position 3 in ionic 2
SyntaxError: Unexpected token < in JSON at position 3 in ionic 2
我在 Ionic 2 "SyntaxError: Unexpected token < in JSON at position 3" 中遇到错误。我的 json 格式使用 spring 引导正确构建。
下面是我的 spring 启动代码。
感谢您的帮助。
@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 来自 /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}]
我的 http://localhost:8080/myview 不工作,因为当我尝试使用数组值的下面代码时它工作。如何调用 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 中提取数据。但在您的情况下,它会引发错误。
我在 Ionic 2 "SyntaxError: Unexpected token < in JSON at position 3" 中遇到错误。我的 json 格式使用 spring 引导正确构建。
下面是我的 spring 启动代码。
感谢您的帮助。
@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 来自 /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}]
我的 http://localhost:8080/myview 不工作,因为当我尝试使用数组值的下面代码时它工作。如何调用 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 中提取数据。但在您的情况下,它会引发错误。