在Springmvc中,前端html传递的经常是json字符串的参数格式,后端用@RequestParam来接收
比如前端html代码
this.searchInfo.side_fid = '001';
getSideviceList(this.searchInfo).then(res => {
console.log("调用成功",res);
});
vue代码
import axios from 'axios';
let baseUrl = '/powersys';
var qs = require('qs');
//获取多条数据
export const getSideviceList = params=>{
let pm=qs.stringify(params);
return axios.post(`${baseUrl}/mweb/sidevice/getList`,pm);
}
此处stringify() 是json方法,用于将 html中的JavaScript 值转换为 JSON 字符串,并传递给后台。
后台代码
// 更新信息
@RequestMapping(value = "/getList", method = RequestMethod.POST)
@ResponseBody
public ResultMsg getList(HttpServletResponse response, @RequestParam HashMap<String, String> map) {
//此处后端接收的map即前端传递的Json格式,可以用map.get("fid")方法获取参数值。
}
如果前端传递的是list数组,格式如
此时就不能转json字符串,而是直接将原值传递到后台,后台必须用@RequestParam接收方式,代码如下
// 添加信息
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody
public ResultMsg addSave(HttpServletResponse response, @RequestBody Object map) {
//注意此处用的是@RequestBody,不是@RequestParam
}