0

I want to save data standard name to database using jQuery with not required form th:action and redirection.

I tried with this but caught error.

If you know the reason for this error please explain here.

Thymeleaf code

<form action="#"  >
<table>
<h1>Create Standard</h1>
<tr>
<td>Standard Name:</td>
<td><input type="text" th:value="${standardName}" placeholder="Enter Standard Name" required="required"id="std" name="stdName"/></td></tr>
<td><input type="submit"  class="btn btn-primary" value="Create" id="savebutton" name="save" /></td>
</table>
</form>

I am using jQuery because of the redirect and form actions are not working here. That's my requirement, so I use jQuery.

jquery

<script type="text/javascript" th:inline="javascript" charset="utf-8"
    src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script type="text/javascript" th:inline="javascript" charset="utf-8"
    src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>

<script>
$(document).ready(function(){
  $("#savebutton").click(function(){

    $.ajax({
        type : 'POST',
        url : "/saveStandards.html ",
            data : ({
                std : standardName //this standardName not defined that is the error 
            })

    });

  });
});
</script>

Controller

  @RequestMapping(value = Array("/saveStandards.html"))
  @ResponseBody
  def saveStandards(@RequestParam std:String) {
  var standard:Standard=new Standard
    standard.setCreatedDate(new java.sql.Date(new java.util.Date().getTime))
    standardService.addStandard(standard)
    println("*****inside controller*****"+std+"****last***")//here std is not printing because of the value not catch @ here

}

error caught:

Uncaught ReferenceError: standardName is not defined

1 Answer 1

4

It is because you do not have a variable declared with name standardName, you can fix as below

$(document).ready(function(){
    $("#savebutton").click(function(){

        $.ajax({
            type : 'POST',
            url : "/learnware/saveStandards.html ",
            data : ({
                std : $('#std').val()
            })

        });

    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

500 (Internal Server Error) occured

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.