如何通过Ajax将Long和Double发送到Spring控制器?

时间:2017-03-29 14:56:40

标签: ajax spring

我想使用Ajax将Long和Double发送给Spring控制器。

  var id = $(this).data('customer-id');
    var quantity = $('#quantity-' + id).val();
    $.ajax({
        headers: {
            "Accept": "application/json",
            "Content-Type": "application/json"
        },
        type: "POST",
        url: '/changeQuantityOrder',
        data: {idOrder: id, quantityChange: quantity},

        error: function(e) {
            console.log(e);
        },
        success: function (msg) {
            window.location.href = "/administrationNotSleeps";
        }

我尝试接收代码:

@RequestMapping(value = "/changeQuantityOrder", method = {RequestMethod.POST})
public @ResponseBody Long editCustomerOrder(@RequestParam(value = "idOrder")Long id,
                                            @RequestParam(value = "quantityChange")Double quantity){       
    System.out.println("Id:"+id+" Quantity "+quantity );
    return id;
}

但我收到异常

{"timestamp":1490775639761,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required Long parameter 'idOrder' is not present","path":"/changeQuantityOrder"}"

我找了并找到了这个异常(MissingServletRequestParameterException)https://stackoverflow.com/
但这篇文章写于3年前,也许有些变化?
因为现在我将String发送到Spring控制器,之后我在java中使用split

1 个答案:

答案 0 :(得分:0)

键应该在引号中     data: {"idOrder": id, "quantityChange": quantity} 还要提一下dataType - 期待什么样的响应。