Search code examples
serializationjquery

$(this).serialize() -- How to add a value?


currently I have the following:

$.ajax({
    type: 'POST',
    url: this.action,
    data: $(this).serialize(),
});

This works fine, however I would like to add a value to data, so I tried

$.ajax({
    type: 'POST',
    url: this.action,
    data: $(this).serialize() + '&=NonFormValue' + NonFormValue,
});

But that didn't post correctly. Any ideas on how you can add an item to the serialize string? This is a global page variable that isn't form specific.


Solution

  • ⛔ 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.