tags:

views:

40

answers:

3

Lets say I want to join two data sets in my $.ajax call as such:

updateData: function(dataDetails) {
    $.ajax({
        url: './example.php',
        data: {
            lets:"GET",
            real:"funky"
        },
        type: "POST",
        dataType: "json",

    });
}

dataDetails in the function argument contains another set of data, such as...

{
    a:"1",
    b:"2",
    c:"3"
}

How should I declare this in my data: area of the $.ajax() if I want to join these sets?

+4  A: 

I believe the $.extend() utility should work here:

data: $.extend({
    lets:"GET",
    real:"funky"
}, dataDetails)
Yi Jiang
+1  A: 

the extend function is great for this, I think.

http://api.jquery.com/jQuery.extend/

it merges the two objects recursively, overwriting some of the parameters of the first, if they are identical.

Oren Mazor
+1  A: 

as Yi Jiang suggested .extend will do the work. But to be on the safer side you can do this to recursively merge them:-

data: $.extend(true,{
    lets:"GET",
    real:"funky"
}, dataDetails) 
A_Var
While not necessarily suitable this is absolutely an important point to make. +1