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调试控制台