< in JSON at position 0 at JSON.parse () error when testing on android device.
我搜索了一下,发现问题与 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": "[email protected]",
"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": "[email protected]",
"password": "613387"
},
{
"id": 18,
"lastname": "chendada",
"firstname": "Jerry",
"email": "[email protected]",
"password": "613387"
},
{
"id": 19,
"lastname": "chendada",
"firstname": "Jerry",
"email": "[email protected]",
"password": "613387"
},
{
"id": 20,
"lastname": "VFVSD",
"firstname": "Jerry",
"email": "DSFSDF",
"password": "FDSFSDFDS"
},
{
"id": 22,
"lastname": "zhaohua liu",
"firstname": "Jerry",
"email": "[email protected]",
"password": "613387"
},
{
"id": 23,
"lastname": null,
"firstname": null,
"email": "[email protected]",
"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 是正确的,就像你们所有人所说的那样。 但是,当我部署应用程序并在设备上远程测试它时: 我得到了 index.html 作为响应:
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'
}
})
}
);
}
@ResponseBody
放在控制器上
@GetMapping("/users")
@ResponseBody
public List getUsers(){
List users = userRepository.findAll();
return users;
}
或
@GetMapping(value="/users",produces="application/json")
@ResponseBody
public List getUsers(){
List users = userRepository.findAll();
return users;
}
或 用
@RestController