【发布时间】:2017-01-17 22:24:23
【问题描述】:
Ionic 2 中出现错误“SyntaxError: Unexpected token
下面是我的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","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而不是将静态值放入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);
}
}
}
}
【问题讨论】:
标签: json spring-mvc spring-boot ionic2