Spring Boot SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (< anonymous>) at XMLHttpRequest.onLoad

nzkunb0c  于 2022-12-29  发布在  Spring
关注(0)|答案(2)|浏览(163)

我希望这不是一个愚蠢的问题。连接离子和java springboot,当应用程序在浏览器中实时服务时没有错误消息,数据正确显示。但得到了语法错误:在Android设备上测试时,JSON中位置0处出现意外标记〈。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;
    }

get请求响应正确记录数据:

[
    {
        "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

重新编辑:
谢谢你们的回复,我是新来的,你们是最棒的。
我发现这里出了什么问题:
当我在浏览器上服务时,我发现响应是正确的,就像所有u guys说的那样。response when serve on a browser
然而,当我部署应用程序并在设备上远程测试时:我得到了index.html作为响应:enter image description here

ewm0tg9j

ewm0tg9j1#

你似乎要取回一个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'
            }
          })
              
              
          }
      );
  }
dy2hfwbg

dy2hfwbg2#

按以下步骤操作:在控制器上放置@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注解类

相关问题