简体   繁体   中英

Am getting 415 unsupported media type error while sending form data to controller

This is the jquery function which returns 415 media type not found error

$('#save').on('click',function(){
    alert("test");
    var data = JSON.stringify(jQuery('#form').serializeArray());
    console.log("data"+data);
    $.ajax({
        type: "POST",
        url: "saveExpenses",
        data: JSON.stringify(jQuery('#form').serializeArray()),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        cache: true,
        success: function(data){alert("Success");}
    })
})

and the controller:

@RequestMapping(value="/saveExpenses",method=RequestMethod.POST)
public String saveExpense(@RequestBody 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";
}

Perhaps you should have a look to produce and consume annotations and attributes. Those posts may certainly help:

Or I don't get it...

EDIT :

  • if the error (415) is thrown from your jquery function then you should certainly also define the "accept: application/json" thing in your original query. Cf. this post .

  • If server-side, you should also check that Jackson is present in your libraries. Cf. this post .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM