I have this ajax code:
return $.ajax({ type: "POST", url: "somefile.php", cache:false, data: { "yfilter": $("#yearFilter").serializeArray(), "gfilter": $("#genreFilter").serializeArray() }, dataType:"json", success: function(data){ alert("success"); }
This works fine, but I need to pass the data parameter dynamically. At the moment, I need the specified content of the data data and one row.
How to transfer it dynamically? / How to save it in a variable and transfer it to the "data:" field?
{ "yfilter": $("#yearFilter").serializeArray(), "gfilter": $("#genreFilter").serializeArray() }
I tried JSON.stringify I could not get it to work, probably due to the data being an array.
Annual and genre arrays come directly from the jquery dropdown menu. It is selected as #id in the same way as "$ (" # yearFilter ")". This is an element of the selected shape.
<select multiple="multiple" name="yearFilter[]" class="filterChange" id="yearFilter">
What I need at a basic level:
var someData = ""; if(...){ someData = { "yfilter": $("#yearFilter").serializeArray(), "gfilter": $("#genreFilter").serializeArray() }; } else if(...){ someData = "sampleString"; }
And in ajax call:
... data: someData, ...
source share