Note that there are some explanatory texts on larger screens.

plurals
  1. POinsert values to database via Ajax - Spring MVC
    primarykey
    data
    text
    <p>I have already read <a href="https://stackoverflow.com/questions/11611843/spring-and-ajax">this</a> and <a href="https://stackoverflow.com/questions/1960240/jquery-ajax-submit-form">this</a> to get some help but there is something wrong in my code. Well I want to insert a form to database via Ajax and this is what i did:</p> <h2>The Ajax function :</h2> <pre><code>&lt;script type="text/javascript"&gt; function doAjaxPost() { var form = $('#ajf'); frm.submit(function () { $.ajax({ type: "POST", url: "${pageContext.request.contextPath}/ajouter_user", data: form.serialize(), success: function(response){ // we have the response $('#info').html(response); }, error: function(e){ alert('Error: ' + e); } }); }); } &lt;/script&gt; </code></pre> <p>'#info' is the ID of the DIV where i want to show the success message returned by the controller.</p> <h2>this is my controller :</h2> <pre><code>@RequestMapping(value="/ajouter_user",method=RequestMethod.POST) public @ResponseBody String addUser(@ModelAttribute User us,BindingResult result,ModelMap model){ String returnText; if(!result.hasErrors()){ model.addAttribute("us", new User()); userservice.AddUser(us); model.addAttribute("usersystem", userservice.getAllUsers()); return returnText = "User has been added to the list." ; }else{ return returnText = "Sorry, an error has occur. User has not been added to list."; } } </code></pre> <h2>HTML :</h2> <pre><code>&lt;form:form id="ajf" method="POST" commandName="user"&gt; Here are my fields ... &lt;input type="submit" value="Créer" onclick="doAjaxPost()"/&gt; &lt;/form:form&gt; </code></pre> <p>What is wrong is : <strong>I don't get the String that the controller return , I get an alert error (object [] object ), the data is inserted to database and the page reload after submitting without giving any error</strong></p> <p>Can someone give me a toturial how to use Ajax with spring (inserting to database ) please help me</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload