• Hi @chouby,
    We have a compatibility issue with Polylang. The problem caused by the following code in polylang/admin/admin-base.php. It seems like Polylang hooks itself in the admin area to the $.ajaxPrefilter to place its query variable to every request. It seems like that problem is that jQuery (probably is happens since WordPress started to use jQuery 3) expects the data to be string instead of object.

    The error can be simple reproduced in the admin area with the following code [only Polylang plugin required]:

    var undef;
    jQuery.ajax({
        type: 'POST',
        url: ajaxurl,
        data: undef
    });

    Original code:

    
    <script type="text/javascript">
        if (typeof jQuery != 'undefined') {
            jQuery(
                function($){
                    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
                        if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) {
    
                            if ( 'undefined' === typeof options.data ) {
                                options.data = ( 'get' === options.type.toLowerCase() ) ? '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>' : <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>;
                            } else {
                                if ( 'string' === typeof options.data ) {
                                    if ( '' === options.data && 'get' === options.type.toLowerCase() ) {
                                        options.url = options.url+'&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
                                    } else {
                                        try {
                                            var o = JSON.parse(options.data);
                                            o = $.extend(o, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
                                            options.data = JSON.stringify(o);
                                        }
                                        catch(e) {
                                            options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>&'+options.data;
                                        }
                                    }
                                } else {
                                    options.data = $.extend(options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
                                }
                            }
                        }
                    });
                }
            );
        }
    </script>
    

    I propose the following fix for this issue:

    
    <script type="text/javascript">
        if (typeof jQuery != 'undefined') {
            jQuery(
                function($){
                    $.ajaxPrefilter(function (options, originalOptions, jqXHR) {
                        if ( -1 != options.url.indexOf( ajaxurl ) || -1 != ajaxurl.indexOf( options.url ) ) {
    
                            if ( 'undefined' === typeof options.data ) {
                                options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
                            } else {
                                if ( 'string' === typeof options.data ) {
                                    if ( '' === options.data && 'get' === options.type.toLowerCase() ) {
                                        options.url = options.url+'&<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>';
                                    } else {
                                        try {
                                            var o = JSON.parse(options.data);
                                            o = $.extend(o, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>);
                                            options.data = JSON.stringify(o);
                                        }
                                        catch(e) {
                                            options.data = '<?php echo $str; // phpcs:ignore WordPress.Security.EscapeOutput ?>&'+options.data;
                                        }
                                    }
                                } else {
                                    options.data = JSON.stringify($.extend(options.data, <?php echo $arr; // phpcs:ignore WordPress.Security.EscapeOutput ?>));
                                }
                            }
                        }
                    });
                }
            );
        }
    </script>
    
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘$.ajaxPrefilter .data should be string’ is closed to new replies.