Jquery $.post not working with Spring MVC 4

148 Views Asked by At

Jquery $ .post not working on a project using Spring MVC 4 in IntelliJ IDEA.

I need to send the data to the Controller Spring MVC when the checkbox is checked or unchecked! But the parameters are being sent always "null".

What am I doing wrong?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
        $(document).ready(function () {
            $('input[id="chTarefa"]').change(function () {
                var id = $(this).attr("idTarefa");
                if ($(this).prop('checked')) {
                    $.post("checkbox.html", {id: id, estado: "marcado"});
                    alert("Checked");
                }
                else {
                    $.post("checkbox.html", {id: id, estado: "desmarcado"});
                }
            });
        });
    </script>

Controller:

@Controller
public class CheckboxController {
    @RequestMapping("checkbox.html")
    public ModelAndView adiciona(String id, String estado) {

        ModelAndView mv = new ModelAndView("checkbox");

        if(estado.equals("marcado")){
            mv.addObject("msg", "Finish!");
        }else{
            mv.addObject("msg", "No finish!");
        }
        return mv;
    }
}

Html:

<input id="chTarefa" type="checkbox" idTarefa="4" /> Checkbox

0

There are 0 best solutions below