Ok so I'm submitting a simple form to my Spring Controller through jQuery. Here is the code:
<form id="addNote" role="form" action="addNote" method="POST">
<div class="form-group error">
<label for="note">Enter your Note </label>
<textarea rows="3" cols="50" name="note" class="form-control"
id="note"></textarea>
</div>
<div class="alert alert-danger" style="display: none">
<strong>Error!</strong> Text length must not exceed 100 characters
</div>
<button type="submit" class="btn btn-default">Save Note</button>
$(document).ready(function(){
$("#addNote").submit(function(e){
e.preventDefault();
var formObj = $(this);
var formURL = "addNote";
var formData = $("#note").val();
$.ajax({
url: formURL,
type: 'POST',
data: formData,
success: function(response){
alert(response);
},
error: function(response){
}
});
});
});
And the Controller method:
@RequestMapping(value="/addNote", method=RequestMethod.POST)
public Vector<String> addNote(Locale locale, Model model, HttpServletRequest httpServletRequest, HttpSession httpSession){
String note = httpServletRequest.getParameter("note");
notes.add(note);
ModelAndView mv = new ModelAndView("notes");
mv.addObject("thenotes", notes);
return notes;
}
I need to return the notes
object to the jQuery so that I can display it's data as output. But this is the error I'm getting in the Chrome console:
So apparently there is a problem in the path. I have tried changing var formURL = "addNote";
to var formURL = "assessment/addNote";
in the jQuery but it doesn't work.
But for some reason if I change return value of addNote()
function in the Controller to ModelAndView
and return mv
then it works but it's not the response in the jQuery I need.