Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for object, array and string data support #12

Open
ghost opened this issue Jun 18, 2012 · 2 comments
Open

Fix for object, array and string data support #12

ghost opened this issue Jun 18, 2012 · 2 comments

Comments

@ghost
Copy link

ghost commented Jun 18, 2012

I wanted to use your plugin at some other plugin I was making, so I took the liberty of fixing the script to allow support for arrays, objects and strings:

Just replace where it says:

// If there is any additional data specified via the `data` option,
// we add it as hidden fields to the form. This (currently) requires
// the `processData` option to be set to false so that the data doesn't
// get serialized to a string.
if (typeof(options.data) === "string" && options.data.length > 0) {
    $.error("data must not be serialized");
}
$.each(options.data || {}, function(name, value) {
    if ($.isPlainObject(value)) {
        name = value.name;
        value = value.value;
    }
    $("<input type='hidden' />").attr({name:  name, value: value})
                                .appendTo(form);
});

With the following:

// Convert data to string, if not already a string
if ( options.data && options.processData && typeof options.data !== "string" ) {
    options.data = jQuery.param( options.data, options.traditional );
}

// Convert string to file inputs to be submitted to the iframe

// Split string by "&"
var splitString= decodeURIComponent( options.data ).split("&");
for ( var i = 0; i < splitString.length; i++ ) {
    // Split substing by "="
    var keyValuePair= splitString[i].split("=");
    // Create input field with name and value of split string
    $("<input type='hidden' />").attr({name: keyValuePair[0], value: keyValuePair[1]})
                                     .appendTo( form );
}

Please add this to the actual plugin!
Also use the fix mentioned on another issue, so that data are submitted in jQuery 1.7+!

@jeremyhaile
Copy link

I've also run into this problem and spent hours pulling my hair out. The problem with trying to fix it on my end is that my data is being set by an ajaxPreFilter. And ajaxPreFilters ONLY allow setting data to a string. You can see the "wontfix" bug report on jQuery here: http://bugs.jquery.com/ticket/9757

That means that this needs to be fixed in iframe-transport.

@groe
Copy link

groe commented Mar 19, 2018

The above code has an issue with form field values containing = characters. Here's my adapted version:

// Convert data to string, if not already a string
if ( options.data && options.processData && typeof options.data !== "string" ) {
    options.data = jQuery.param( options.data, options.traditional );
}

// Convert string to file inputs to be submitted to the iframe

// Split string by "&"
var splitString= options.data.split("&");
for ( var i = 0; i < splitString.length; i++ ) {
    // Split substing by "="
    var keyValuePair = splitString[i].split("=");
    // Create input field with name and value of split string
    $("<input type='hidden' />").attr({
                                        name: decodeURIComponent(keyValuePair[0]),
                                        value: decodeURIComponent(keyValuePair[1])
                                      })
                                     .appendTo( form );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants