0

Am getting form data in console.log

$('#save').on('click',function(){
alert("test");
var data = $('form').serialize();
console.log("data"+data);
$.ajax({
    type: "POST",
    url: "saveExpenses",
    data: $('form').serialize(),
    cache: true,
    success: function(data){
        alert("Success");
    }
  })
})

and the controller:

@RequestMapping(value="/saveExpenses",method=RequestMethod.POST)
public String saveExpense(ExpensesSummary expenses, HttpServletRequest request,HttpSession session){
    System.out.println("first name"+expenses.getFirstName());
    String message = homeBankingDao.expenseSummary(expenses);
    request.getSession().setAttribute("message",message);
    return "login";

}

but am getting 400 bad request error.can anyone help me out in this..

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
Yuvanath
  • 127
  • 3
  • 11
  • `saveExpense` takes `ExpensesSummary expenses` but will instead get the serialized form data as a string. Also, how will the other 2 arguments be passed in? – Scott Marcus Oct 17 '17 at 17:30
  • Simple way to fix 400 erorr! :) https://www.lifewire.com/how-to-fix-a-400-bad-request-error-2617988 – nagendra547 Oct 17 '17 at 17:32
  • nope other two will not be.. @ScottMarcus – Yuvanath Oct 17 '17 at 17:34
  • if data .serialize wont work means..can u suggest how to sent the form data to controller @ScottMarcus – Yuvanath Oct 17 '17 at 17:35
  • You are sending the data to the controller, but the controller appears to want a type that HTML can't send. HTML only deals in strings, not `ExpenseSummary`s – Scott Marcus Oct 17 '17 at 17:43
  • Dont you need the context path for your url attribute `url: "{appName}/saveExpenses"`. Also if you are using jackson use the @RequestBody annotaion for your `ExpenseSummary` object. This will help for the conversions https://stackoverflow.com/questions/11291933/requestbody-and-responsebody-annotations-in-spring – Neero Oct 17 '17 at 17:48
  • i added @requestbody...but it throwing unsupported mediatype error 415 – Yuvanath Oct 17 '17 at 18:01

0 Answers0