语法错误:XMLHttpRequest.onLoad 处出现意外标记 < in JSON at position 0 at JSON.parse (<anonymous>)

问题描述 投票:0回答:2
希望这不是一个愚蠢的问题。 连接 ionic 和 java springboot ,当应用程序在浏览器中实时服务并且数据正确显示时,没有错误消息。但得到了 SyntaxError: Unexpected token

< 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 是正确的,就像你们所有人所说的那样。

response when serve on a browser

但是,当我部署应用程序并在设备上远程测试它时: 我得到了 index.html 作为响应:

enter image description here

angular spring-boot ionic4
2个回答
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' } }) } ); }


0
投票
执行以下操作: 将

@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

 注释你的课程
    

© www.soinside.com 2019 - 2024. All rights reserved.