I have one problem with send my form.
My form looks like:
<form method="post" onsubmit="saveName()" modelAttribute="task" id="formName" role="form">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" />
</form>
My Controller looks like:
@Controller
@RequestMapping(value="/form")
public class restController {
@RequestMapping(value="/take", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public void getFormName(@RequestParam("name") String nameTask){
myFunction(nameTask);
}
}
and I would like using Javascript to send parameters to REST. If I do it directly, using the "action" for the form, everything works. However, I would like to write a simple script that will provide all the parameters to the Controller
I tried to write a script but it did not work:
SaveName script:
req = new XMLHttpRequest();
req.open('POST', encodeURI('/form/take'), true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(encodeURI('name=' + $("#name").val()));
Any sugestions?