1

I have tried to send one of the key to data parameter as anonymous function, but whatever datatype it returns, Jquery doesn't pass it to server.

Examples:

$.ajax({
    type: 'post',
    url: 'http://host.com/cart',
    dataType: 'html',
    data: {
        'quantity': function () {
            var return_obj = [];
            return_obj['1:YTowOnt9'] = '1';
            return return_obj;
        },
        'action': 'cart-update-quantity'
    },
    success: function(data) {
        console.log(data);
    }
});

this has the action POST key, however quantity key is empty string. I tried also as string and object.

This example is working:

$.ajax({
    type: 'post',
    url: 'http://host.com/cart',
    dataType: 'html',
    data: {
        'quantity': {
            '1:YTowOnt9': '1'
        },
        'action': 'cart-update-quantity'
    },
    success: function(data) {
        console.log(data);
    }
});

How can I send anonymous function to quantity key in data? The anonymous function will get some of the form inputs dinamically. I also have tried jQuery serializeArray, but it send the data wrong.

3
  • Why do you want to put a function there? Just pass a dynamic object that is built before the ajax call. Commented Jul 7, 2014 at 11:01
  • 2
    Don't abuse arrays! Especially jQuery will choke on that. Commented Jul 7, 2014 at 11:02
  • @Bergi: Thanks for pointing that out. For me, Array.length equals zero to non number keys is a big point. Commented Jul 7, 2014 at 11:26

1 Answer 1

5

You do not need to defer execution of the anonymous function, so just call it immediately to return your object:

$.ajax({
    type: 'post',
    url: 'http://host.com/cart',
    dataType: 'html',
    data: {
        'quantity': (function () {
            var return_obj = {};
            return_obj['1:YTowOnt9'] = '1';
            return return_obj;
        })(),
        'action': 'cart-update-quantity'
    },
    success: function(data) {
        console.log(data);
    }
});

This is called an IIFE (immediately Invoked function expression).

Alternatively, just build the object before the Ajax call.. In any case the error was down to passing a function and not the result of the function.

*Note: This code includes an edit by Bergi to create an object (with {}) instead of an array (with [])

You can of course simply that IIFE to an anonymous object declaration:

'quantity': (function () {
        return {'1:YTowOnt9': '1'};
    })(),
Sign up to request clarification or add additional context in comments.

2 Comments

Your code functioned right away. I also found this as the same example. I was thinking that self invoked functions will invoke themselves only once. However having the ajax in a function will invoke the IIFE every time I invoke the main function, correct?
Self invoked functions get called based on where they are. If in a loop or function they get called each time. You often see them as globals in which case they only get called once. Cheers.

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.