希望这不是一个愚蠢的问题。 连接ionic和java springboot时,当应用在浏览器中实时提供服务并且数据正确显示时,没有错误消息。但是出现了SyntaxError:在Android设备上进行测试时,JSON中的意外令牌<在JSON位置0处的JSON.parse()错误。
我四处搜索,发现问题与JSON解析有关,但我不太理解为什么浏览器中未报告错误消息。
我想在这里做什么: 我有一个具有用户名和电子邮件地址的用户表,我正在连接以在前端显示所有名称。后端返回一个列表。
我还使用了chrome CROS扩展名,但我认为这根本没有关系。 有人可以帮忙解释一下吗?谢谢
typescript
ngOnInit() {
this.surgeonList = [
];
console.log(this.value);
this.httpclient.get('http://localhost:8080/users'
).subscribe(
data => {
data = JSON.parse(JSON.stringify(data));
for (var i = 0 ; i < (<Array<any>>data).length; i++) {
this.surgeonList.push(
{
name: data[i].lastname,
icon: 'person'
}
);
}
}
);
}
JAVA spring controller
@GetMapping("/users")
public List getUsers(){
List users = userRepository.findAll();
return users;
}
获取请求响应正确记录了数据:
[
{
"id": 2,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen698698@gmail.com",
"password": "613387"
},
{
"id": 3,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": null
},
{
"id": 4,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": null
},
{
"id": 5,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": null
},
{
"id": 6,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": null
},
{
"id": 7,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 8,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 9,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 10,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 11,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 12,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 13,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 14,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 15,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 16,
"lastname": "Liu",
"firstname": "Jerry",
"email": "karen77777",
"password": "123333"
},
{
"id": 17,
"lastname": "chenniuniu",
"firstname": "Jerry",
"email": "chenniuniu@gmail.com",
"password": "613387"
},
{
"id": 18,
"lastname": "chendada",
"firstname": "Jerry",
"email": "chendaada@bobo.com",
"password": "613387"
},
{
"id": 19,
"lastname": "chendada",
"firstname": "Jerry",
"email": "chendaada@bobo.com",
"password": "613387"
},
{
"id": 20,
"lastname": "VFVSD",
"firstname": "Jerry",
"email": "DSFSDF",
"password": "FDSFSDFDS"
},
{
"id": 22,
"lastname": "zhaohua liu",
"firstname": "Jerry",
"email": "zliu633@uwo.ca",
"password": "613387"
},
{
"id": 23,
"lastname": null,
"firstname": null,
"email": "yche98@uwo.ca",
"password": "613388"
}
]
但是在chrome上进行远程检查时得到了
vendor.js:44729 ERROR
HttpErrorResponse
error:
error: SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad
重新编辑:
谢谢你们的答复我是社区的新成员,你们是最棒的。
我发现这里有什么问题:
当我在浏览器上投放内容时,我发现响应作为json是正确的,就像你们所有人都说的那样。 response when serve on a browser
但是,当我部署该应用程序并在设备上对其进行远程测试时: 我得到index.html作为响应: enter image description here
答案 0 :(得分:0)
您似乎正在找回JSON对象,然后重新串化并解析它。那可能就是为什么?
ngOnInit() {
this.surgeonList = [
];
console.log(this.value);
this.httpclient.get('http://localhost:8080/users'
).subscribe(
data => {
this.surgeonList = data.map(doc=>{
return {
doc.lastname,
icon: 'person'
}
})
}
);
}
答案 1 :(得分:0)
执行以下操作:
将@ResponseBody
放在您的控制器上
@GetMapping("/users")
@ResponseBody
public List getUsers(){
List users = userRepository.findAll();
return users;
}
OR
@GetMapping(value="/users",produces="application/json")
@ResponseBody
public List getUsers(){
List users = userRepository.findAll();
return users;
}
或
使用@RestController