$(this).serialize() -- How to add a value?
While matt b's answer will work, you can also use .serializeArray()
to get an array from the form data, modify it, and use jQuery.param()
to convert it to a url-encoded form. This way, jQuery handles the serialisation of your extra data for you.
var data = $(this).serializeArray(); // convert form to array
data.push({name: "NonFormValue", value: NonFormValue});
$.ajax({
type: 'POST',
url: this.action,
data: $.param(data),
});
Instead of
data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
you probably want
data: $(this).serialize() + '&NonFormValue=' + NonFormValue,
You should be careful to URL-encode the value of NonFormValue
if it might contain any special characters.
firstly shouldn't
data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
be
data: $(this).serialize() + '&NonFormValue=' + NonFormValue,
and secondly you can use
url: this.action + '?NonFormValue=' + NonFormValue,
or if the action already contains any parameters
url: this.action + '&NonFormValue=' + NonFormValue,