0

I want to send Long and Double to Spring controller use Ajax.

  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";
        }

I try to receive a code so:

@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;
}

But I receive exception

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

I looked for and have found about this exception (MissingServletRequestParameterException) https://stackoverflow.com/
But this article was written 3 years ago, maybe something changed?
Because now I send String to Spring controller and after that I use split in java.

Community
  • 1
  • 1
Viking
  • 177
  • 5
  • 16

1 Answers1

0

key should be in quotes data: {"idOrder": id, "quantityChange": quantity} Also mention dataType - what kind of response to expect.

Puppala Mounika
  • 103
  • 1
  • 1
  • 4