How do I convert this Ajax function (in prototype) to a jquery one?

196 views Asked by At

Can anyone please help me to translate this function in prototype to the equivalent in jQuery

function updateNewsletter(){                  
    if ($('newsletter_dump')){
        $('newsletter_dump').remove();
    }
    newsletterParam = $('newsletter_form').serialize(true);
    newsletterParam.template_output = 'box/plugin_newsletter';

    new Ajax.Updater('newsletter_form_holder', 'index.php', {
        parameters: newsletterParam,
        evalScripts: true
    });
}

Thanks is advance.

I tried this code but not working. I keep getting an object error

function updateNewsletter(){                  
    if ($('#newsletter_dump')){
        $('#newsletter_dump').remove();
    }
    newsletterParam = $('#newsletter_form').serialize(true);
    newsletterParam.template_output = 'box/plugin_newsletter';
    $.ajax({
        type: 'GET',
        url: 'index.php',
        data: {"newsletterParam" : "newsletter_form_holder"},
        dataType: 'script',
        success: function(data){
            alert(data);
        },
        error: function(e){
            alert(e);
        }
    });
}

The problem may come from newsletterParam.template_output = 'box/plugin_newsletter'; Any idea on how to add another form element to the serialised one in jQuery? Thanks

1

There are 1 answers

0
Don Bottstein On BEST ANSWER

Unlike Prototype's serialize function, jQuery's serialize function returns a string only. Your error is due to the fact that you are using newsletterParam as an object rather than a string. So to fix the problem just append the template_output parameter as a string:

newsletterParam = $('newsletter_form').serialize();
newsletterParam += '&template_output=box/plugin_newsletter';

Also, the data setting in your ajax call should be

data: newsletterParam,