$.ajaxPrefilter() may not work as expected
-
Hello Chouby.
In a plugin I’m doing this:
$.getJSON( ajaxurl + "?" + $myForm.serialize(), youGotIt ).fail( pebcak );In
PLL_Admin::admin_print_footer_scripts()you’re using$.ajaxPrefilter()and you testoptions.datatype.The problem:
The string “&[object%20Object]” is appended to the requested URL.
The good news is nothing breaks in my plugin.
The bad news is your parameter is not sent.Why:
As you can see, I’m not using the “data” parameter in $.getJSON().
As a result in your$.ajaxPrefilter(),options.datatype is not a string, but “undefined”, sooptions.databecomes an object, but I use “get”, so the object is appended directly to the URL.Patch:
if (typeof jQuery != 'undefined') { (function($){ $.ajaxPrefilter(function (options, originalOptions, jqXHR) { if ( typeof options.data === 'undefined' ) { if ( originalOptions.type === "get" ) { options.data = '<?php echo $str;?>'; } else { options.data = {<?php echo $arr;?>}; } } else if (typeof options.data === 'string' ) { options.data = '<?php echo $str;?>'+options.data; } else { options.data = $.extend(options.data, {<?php echo $arr;?>}); } }); })(jQuery) }I haven’t tried with a “post” call, but I guess
originalOptions.typewill be set to “post” in that case, and depending of this value I send a string or an object. I guess there’s room for improvements in this code, I think we could skip the type test part (for string). and use theoriginalOptions.typetest for everything.Right now, my quick fix in my plugin is to provide a data parameter:
$.getJSON( ajaxurl + "?" + $importForm.serialize(), "", acvsiPostSynchroSuccess ).fail( acvsiPostSynchroFail );Cheers, Greg.
The topic ‘$.ajaxPrefilter() may not work as expected’ is closed to new replies.