• Plugin Contributor Grégory Viguier

    (@greglone)


    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 test options.data type.

    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.data type is not a string, but “undefined”, so options.data becomes 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.type will 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 the originalOptions.type test 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.

    https://wordpress.org/plugins/polylang/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Chouby

    (@chouby)

    Hi Greg!

    Thanks for the report. I will for sure add this fix.

    I am not a great jQuery specialist and I wonder if there is a specific reason why you use originalOptions.type rather than options.type. I am not sure if the first is always defined while I expect the second to always be.

    I wrote a version a bit less verbose using options.type. Would it be OK?

    if (typeof jQuery != 'undefined') {
    	(function($){
    		$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    			if ( typeof options.data === 'undefined' ) {
    				options.data = options.type === "get" ? '<?php echo $str;?>' : options.data = {<?php echo $arr;?>};
    			}
    			else {
    				options.data = typeof options.data === "string" ? '<?php echo $str;?>'+options.data : $.extend(options.data, {<?php echo $arr;?>});
    			}
    		});
    	})(jQuery)
    }

    Plugin Contributor Grégory Viguier

    (@greglone)

    I wonder if there is a specific reason why you use originalOptions.type rather than options.type.

    Yes, there is a very good reason: I simply have not had a look at options.type. x)

    I wrote a version a bit less verbose using options.type. Would it be OK?

    Almost, a copy/paste mistake in the first part.

    if (typeof jQuery != 'undefined') {
    	(function($){
    		$.ajaxPrefilter(function (options, originalOptions, jqXHR) {
    			if ( typeof options.data === 'undefined' ) {
    				options.data = options.type === "get" ? '<?php echo $str;?>' : {<?php echo $arr;?>};
    			}
    			else {
    				options.data = typeof options.data === "string" ? '<?php echo $str;?>'+options.data : $.extend(options.data, {<?php echo $arr;?>});
    			}
    		});
    	})(jQuery)
    }

    Plugin Author Chouby

    (@chouby)

    That will go like this in the next version. Thanks again 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘$.ajaxPrefilter() may not work as expected’ is closed to new replies.