uni-app后端收到前端数据为null

问题与解决措施:

实现注册功能,插入一条新数据,包括用户的用户名、密码和手机号。使用uni.request请求传数据到后端接口。接口名称为http://localhost:8080/register,是POST请求。但是一开始后端收到的数据为null,查看前端数据是没有问题的,数据类型都是String类型。经过一番查阅之后发现需要加上请求头才能使后端正确接收到数据。

前端:

uni.request({
    url: 'http://localhost:8080/register',
    method: 'POST',
    data: {
        name: this.username,
        password: this.password,
        phone: this.phone
    },
    header: {
    	'content-type': 'application/x-www-form-urlencoded'
    },
    success: (res) => {
        uni.showToast({
            title: '注册成功',
            icon: 'success'
        })
        uni.navigateBack()
    },
    fail: () => {
        uni.showToast({
            title: '注册失败',
            icon: 'error'
        })
    }
})

重要:

header: {
    'content-type': 'application/x-www-form-urlencoded'
}

后端:

@PostMapping("/register")
public void register(String name, String password, String phone) {
    userManageService.addNewUser(name, password, phone);
}

注意:

后端我并没有加@ResponseBody注解,但是为了不让程序解析为页面报错,必须将控制器的注解从@Controller改为@RestController,这一点值得注意。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值