I want to return String from Spring MVC Controller to Ajax . It does not work as expected and gives an error.
My Ajax codes for this:
function ajaxRequest(item) { $.ajax({ type: "POST", url: "/myPage", data: { item: item }, success: function (html) { alert(html); }, error: function(e) { console.log("Error:" + e); } }); }
My controller:
@RequestMapping(value = "/myPage", method= RequestMethod.POST, produces="text/plain") public @ResponseBody String myController(HttpServletRequest request) { String myItem = request.getParameter("item"); ... return myItem + "bla bla bla"; }
Chrome console result:
POST http://localhost:8080/myPage 406 (Not Acceptable) jquery.js Error:[object XMLHttpRequest]
What am I missing here?
source share