前端传递url参数中有中文,后端传过来的有乱码,解决方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sz15732624895/article/details/82946484

一、问题重现:

原代码:用get方式传递

    @ResponseBody
    @RequestMapping(value = {"/findGroupByGroupName/{batchNo}/{groupSex}/{groupName}"}, method = RequestMethod.GET)
    @ApiOperation(value = "模糊搜索查询分组情况是否成功", notes = "输入分组的批次,分组的性别、分组的部分名称", tags = {"查询"})
    public ItooResult findGroupByGroupName(@ApiParam(name = "batchNo", value = "分组的批次", required = true) @PathVariable String batchNo, @ApiParam(name = "groupSex", value = "分组的性别", required = true) @PathVariable String groupSex, @ApiParam(name = "groupName", value = "分组的部分名称", required = true) @PathVariable String groupName) {
       .....
    }

url传递“groupName”为中文时,会显示乱码:

二、解决办法:

url传参去掉“groupName”参数,改为POST方式传参,为“groupName”加上@RequestBody 注解。

修改后代码:

    @ResponseBody
    @RequestMapping(value = {"/findGroupByGroupName/{batchNo}/{groupSex}"}, method = RequestMethod.POST)
    @ApiOperation(value = "模糊搜索查询分组情况是否成功", notes = "输入分组的批次,分组的性别、分组的部分名称", tags = {"查询"})
    public ItooResult findGroupByGroupName(@ApiParam(name = "batchNo", value = "分组的批次", required = true) @PathVariable String batchNo, @ApiParam(name = "groupSex", value = "分组的性别", required = true) @PathVariable String groupSex, @ApiParam(name = "groupName", value = "分组的部分名称", required = false) @RequestBody String groupName) {
    
    ......
1}
    

前端代码:用post传递body形式

selectGroup() {
    searchGroup: string;
    selectGroupUrl = 'physical-web/group/findGroupByGroupName/' + batchNo + '/' + this.studentSex;
    let body = searchGroup;
    this.http.post(selectGroupUrl, body).subscribe(
      res => {
        if (res.json().code == '0000' && res.json().data.length > 0) {
          this.searchGroup = res.json().data;
          for (let i = 0; i < this.searchGroup.length; i++) {
            this.group.id = this.searchGroup[i].id;
            this.group.groupName = this.searchGroup[i].groupName;
          }
        }
        else {
          mui.alert("查询结果为空,请重新输入查询条件");
        }
      }
    )
  }

url 传递的值:

问题解决!

猜你喜欢

转载自blog.csdn.net/sz15732624895/article/details/82946484