How to simply submit a query parameter using jquery form?

I can submit the form as Postusing the following in my javascript:

$("#receiptsForm").submit();

But I also want to send through the parameter myparamalso a request that I will receive in my spring controller using httpServletRequest.getParameter("myparam"):

var myparam = "abc";
$("#receiptsForm").submit();

What is the best I can do?

+4
source share
5 answers

try it

function form_submit()
{
      //var myparam = "abc";

     // add hidden field to your form name="myparam" and value="abc"
      $('#receiptsForm').append('<input type="hidden" name="myparam " value="abc" />');
      $("#receiptsForm").submit(); 
}
+4
source

Try it,

var input = $("<input>")
               .attr("type", "hidden")
               .attr("name", "mydata").val("bla");
$('#receiptsForm').append($(input));
$('#receiptsForm').submit();
+4
source

, jquery. . , ajaxrequest

var myparam = "abc";

var data_to_be_sent = {"myparam":myparam};

ajax

data : data_to_be_sent.

, myparam

+2

serializeArray

var data = $('#receiptsForm').serializeArray();
data.push({name: 'myparam', value: 'MyParamValue'});

data :

$.ajax({
    ...
     data: data,
    ...
});
+1

There are two ways for spring:

a) specify the hidden form field myparamin your form and use jquery to fill it abcin before submitting.

b) use jquery ajax to call ajax post, and before that set the data parameter.

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
+1
source

Source: https://habr.com/ru/post/1524995/


All Articles