4

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. For now I need the above data parameter content and a single string.

How do I pass this dynamically? / How do I store it in a variable and pass it to the "data:" field?

 { "yfilter": $("#yearFilter").serializeArray(),
    "gfilter": $("#genreFilter").serializeArray() }

I tried JSON.stringify I I couldn't get it to work probably due to the data being an array.

The year and genre arrays are coming directly from the jquery drop down menu. It is selected like by it's #id like so "$("#yearFilter")". This is the select form element.

<select multiple="multiple" name="yearFilter[]" class="filterChange" id="yearFilter">

What I need at the base level is:

var someData = "";


    if(...){
        someData = { "yfilter": $("#yearFilter").serializeArray(),
                "gfilter": $("#genreFilter").serializeArray() };
    }
    else if(...){
        someData = "sampleString";
    }

And in ajax call:

...
data: someData,
...
25
  • what do you mean by dynamically..? Commented Oct 28, 2012 at 13:31
  • I mean that I need to pass a variable with either the year/genre data or a single string to the data field depending what content I want displayed. Commented Oct 28, 2012 at 13:32
  • 1
    Create a array and send none if not present ? Commented Oct 28, 2012 at 13:33
  • Yes, but the issue is that I am passing 2 arrays: genre and year. Commented Oct 28, 2012 at 13:34
  • 1
    also shoudln't need traditional:false option Commented Oct 28, 2012 at 13:45

4 Answers 4

11

I think I have an idea what you want but post has been overly complicated by extraneous issues like json stringify . Here's a function that could be used in several places eleswhere in your code to make one type of AJAX call or another.

You would then perhaps have several buttons and call the function within handlers for each type of button and change the argument passed to function

doAjax('someval');/* use in button 1*/
doAjax('someOtherval');/* use in button 2*/



function doAjax(arg) {

    var someData = "";
    if (arg == 'someval') {
        someData = {
            "yfilter": $("#yearFilter").val(),
            "gfilter": $("#genreFilter").val()
        };
    } else {
        someData = "sampleString";
    }

    $.ajax({
        type: "POST",
        url: "somefile.php",
        cache: false,
        data: someData,
        dataType: "json",
        success: function(data) {
            if (arg == 'someval') {
                alert("success 1");
            } else {
                alert("success 2");
            }
        }
    })
}
Sign up to request clarification or add additional context in comments.

1 Comment

Agreed, I assumed it couldn't be achieved by simply moving the current arrays in the same format to a variable. Thanks for getting to the bottom of it!
1

Hope I've understood what you're asking for. You could do something like this:

var parameters = {};
if (...) {
    parameters = $("#yearFilter").serializeArray();
}
if () {
    parameters = $("#genreFilter").serializeArray();
}

and then replace the line:

parameters: { "yfilter": $("#yearFilter").serializeArray(),
              "gfilter": $("#genreFilter").serializeArray() },

with:

data: parameters,

2 Comments

...but i'm not sure if this was what you were asking for!
It's close but it won't work because I need to pass BOTH year and genre arrays at the same time :)
0

JSON type should be best option for dynamically data. push whatever data you wish to inside json like shown below, Hence create your json dynamically and send as ajax data.

var employees = {
    accounting: [],
    dept: "HR"
}; 

employees.accounting.push({ 
   "firstName" : item.firstName,
   "lastName"  : item.lastName,
   "age"       : item.age 
});


$.ajax({
        url: POSTURL,
        type: 'POST',
        dataType: 'json',
        data : employees,
        contentType: 'application/json; charset=utf-8',
        success: function (results)
        {

        },
        error: function (results)
        {
            jQuery.error(String.format("Status Code: {0}, ", results.status, results.statusText));
        }
    });

Comments

0

Try it:

someData = JSON.stringify({ yfilter: $("#yearFilter").val(), gfilter: $("#genreFilter").val() });

It will work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.