Search code examples
jqueryjquery-post

Is there a way to use query string and object, together, inside $.post data argument?


I'm using $.post to send data to the server. There are 4 values that I want to send, two from a form, and two that I've written inside the function:

    var desc = $('textarea[name="image_description"]').fieldSerialize();
    var album = $('select[name="album"]').fieldSerialize();
and the other two I was just putting into an Object
{a:"something", b:"something else"}

I know I could just get the values of the form elements and add them to the object that way, but I was wondering if there was an easy way to combine these two types (ie query string and object) into the data argument?


Solution

  • .serialize() return string which means you can append your additional values from functions.

    var data = $("#yourForm").serialize() + "&a=something&b=else";
    

    I think it is easy and enough to solve your question but if you need more special way to do it, take a look at http://api.jquery.com/jQuery.param/.

    so you can do things like

    var myObject = {
     a: "something",
     b: "else"
    };
    
    var moreValues = $.param(myObject);
    
    var data = $("#yourForm").serialize() + "&" + moreValues;