前端JS传递参数到后端接口问题及解决方案
根据代码中给出的信息,可以看出前端代码中的ajax请求传递的参数是一个包含'mTypeId'属性的对象,并将其转换为JSON字符串进行传递。而在后端的'list'接口中,虽然参数为'VmMineralType'类型的对象,但并没有对传递的参数进行处理,而是直接查询了所有数据。
为了根据'mTypeId'属性进行查询,需要在'list'接口中对传递的参数进行处理,可以使用'@RequestParam'注解指定请求参数名,然后根据参数进行查询,示例如下:
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(@RequestParam("mTypeId") Integer mTypeId)
{
startPage();
VmMineralType vmMineralType = new VmMineralType();
vmMineralType.setMTypeId(mTypeId);
List<VmMineralType> list = vmMineralTypeService.selectVmMineralTypeList(vmMineralType);
return getDataTable(list);
}
这样就可以根据前端传递的'mTypeId'参数进行查询了。另外,前端可以通过将'mTypeId'作为参数传递给ajax请求的'data'属性,然后在后端的'list'接口中使用'@RequestParam'注解获取参数值,示例如下:
前端代码:
$.ajax({
url:ctx+"warehousing/mineraltype/list",
type:"post",
async: false,
data: {
mTypeId: 1
},
success: function(result) {
//...
}
});
后端代码:
@PostMapping("/list")
@ResponseBody
public TableDataInfo list(@RequestParam("mTypeId") Integer mTypeId)
{
//...
}
原文地址: https://www.cveoy.top/t/topic/nALq 著作权归作者所有。请勿转载和采集!