質問

I am able to submit a form as Post type using the following in my javascript:

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

But I also want send across a parameter myparam as well with the request which I'll be retrieving in my spring controller using httpServletRequest.getParameter("myparam"):

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

What's the best I can do?

役に立ちましたか?

解決

try this

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(); 
}

他のヒント

Try this,

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

Try using serializeArray

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

You can send the data like:

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

Looks like you are using jquery..there are a couple of ways this can be done..either you can make it a object and then pass it as data in an ajaxrequest

var myparam = "abc";

var data_to_be_sent = {"myparam":myparam};

then in the data field of the ajax request, you can

data : data_to_be_sent.

Or you simply have a hidden field in the form and then on submit you can give it a value of myparam

There are two ways that spring to mind

a) include a hidden form field called myparam in your form, and the use jquery to populate it with abc before submitting.

b) use Jquery's ajax to call a ajax post, and before doing this set the data parameter.

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top