我正在尝试检索 Java Spring MVC 中的 javascript 对象列表,但是当它到达 Controller 时是空的。
我看了很多书,但我不知道它是如何工作的
这是我的 ajax 代码:
$.ajax({
url : 'ajax/abrir_cotizacion',
data : {
listaunidades: [{
idtipounidad: 1,
modelo: 2013,
cantidad: 1,
tipopago: 1,
costounidad: 1500000,
ivaoperacion: 25000,
totaloperacion: 1504000,
enganche: 750000,
idplazo: 3
},
{
idtipounidad: 2,
modelo: 2012,
cantidad: 2,
tipopago: 2,
costounidad: 1500000,
ivaoperacion: 25000,
totaloperacion: 1504000,
enganche: 750000,
idplazo: 6
}]
},
type : 'POST',
dataType : 'json',
success : function(data) {
},
error : function(xhr, status) {
alert('Disculpe, existió un problema');
},
complete : function(xhr, status) {
//alert('Petición realizada');
}
});
下面是我需要的 Controller 和模型。
@ResponseBody
@RequestMapping(value = "/ajax/abrir_cotizacion", method = RequestMethod.POST)
public Object abrircotizacion(Model model, HttpServletRequest request, @ModelAttribute ArrayList<Unidades> listaunidades) {
try {
Injector inj = AppInjector.getInjector();
return new MsgPojo(1, "Se abre la cotización");
} catch (Exception ex) {
LoggerUtils.printLog(this.getClass(), Level.SEVERE, ex, null, Thread.currentThread().getStackTrace());
return new MsgPojo(-1, "Ocurrio un error al cargar los datos. " + ex.toString());
}
}
public class Unidad {
private int idtipounidad;
private int modelo;
private int cantidad;
private int tipopago;
private double costounidad;
private double ivaoperacion;
private double totaloperacion;
private double enganche;
private int idplazo;
//getters and setters
}
最佳答案
我正在使用 Spring Boot 2.1.4
我已经试过了,它正在工作。
HomeController.class
@RestController
public class HomeController {
private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
@PostMapping("/test")
public void test(@RequestBody UserDTO[] userDTO) {
logger.info(userDTO.length + ""); //expect 2
}
}
UserDTO.class
@Data
public class UserDTO {
private String email;
private String nickname;
}
我正在使用 postman。
POST /test? HTTP/1.1
Host: localhost
Content-Type: application/json
cache-control: no-cache
[
{
"email": "test@test.co.kr",
"nickname": "test1"
},
{
"email": "test2@test.co.kr",
"nickname": "test2"
}
]
和输出:
2019-05-24 11:04:40.871 INFO 1860 --- [p-nio-80-exec-5] k.c.k.w.w.s.controller.HomeController : 2
我还调试以检查我的 userDTO。 电子邮件和昵称设置为我在 postman 中发送的参数。
像这样改变参数:
[
{
"idtipounidad": 1,
"modelo": 2013,
"cantidad": 1,
"tipopago": 1,
"costounidad": 1500000,
"ivaoperacion": 25000,
"totaloperacion": 1504000,
"enganche": 750000,
"idplazo": 3
},
{
"idtipounidad": 2,
"modelo": 2012,
"cantidad": 2,
"tipopago": 2,
"costounidad": 1500000,
"ivaoperacion": 25000,
"totaloperacion": 1504000,
"enganche": 750000,
"idplazo": 6
}
]
和你的 Controller :
@ResponseBody
@RequestMapping(value = "/ajax/abrir_cotizacion", method = RequestMethod.POST)
public Object abrircotizacion(Model model, HttpServletRequest request, @RequestBody Unidad[] unidad) {
....
}
我希望这个工作.. 并尝试 postman 。这很有用。
关于javascript - 将 javascript 对象列表发送到 Spring MVC 中的 Controller 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284534/