uniapp接收服务器消息,uniapp发送请求服务器接收不到数据的问题

官网API文档是这样的

73abe8bfe6a4

发送给服务器的数据格式

方法一:

一般的请求方式

uni.request({

url: this.$serverUrl + "system/mobile/login",

method: "POST",

header: {

'content-type': 'application/x-www-form-urlencoded'

},

data: {

username: this.loginFormData.username,

password: this.loginFormData.password

},

success: (res) => {

console.log(res)

}

})

后端接收

@RequestMapping(value = "/system/mobile/login", method = RequestMethod.POST)

@ResponseBody

public Map mobileLogin(@RequestParam("username") String username, @RequestParam("password") String password) {

System.out.println("username" + username);

System.out.println("password" + password);

Map res = new HashMap<>();

res.put("user", username);

res.put("pwd", password);

return res;

}

方法二:

前端请求是这样的(默认的content-type是application/json格式),url填你自己的url地址,后端接口地址同理

uni.request({

url: this.$serverUrl + 'mobile/login',

data: {

username: 'admin',

password: 'admin'

},

method: 'POST',

dataType: 'JSON',

sslVerify: false,

success: (res) => {

console.log(res)

},

fail: (res) => {

console.log(res)

},

complete: (res) => {

console.log(res)

}

})

所以后端需要接收json格式的数据,一种形式是利用封装好的实体类进行接收,另一种是利用@RequestBody Map map接收,现在采用后面一种形式进行后台数据接收。

@RequestMapping(value = "/mobile/login")

@ResponseBody

public Map mobileLogin(@RequestBody Map data) {

String id = data.get("username");

String pwd = data.get("password");

System.out.print("mLogin id: " + data.get("username") + " pwd " + data.get("password"));

Map res = new HashMap<>();

res.put("user", id);

res.put("pwd", pwd);

return res;

}

这样就能接收到数据了

73abe8bfe6a4

HBuilder X App调试控制台

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值