jquery-iframe-transport
jquery-iframe-transport copied to clipboard
Fix for object, array and string data support
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+!
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.
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 );
}