Title: $.ajaxPrefilter .data should be string
Last modified: February 5, 2021

---

# $.ajaxPrefilter .data should be string

 *  [Nextendweb](https://wordpress.org/support/users/nextendweb/)
 * (@nextendweb)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/ajaxprefilter-data-should-be-string/)
 * Hi [@chouby](https://wordpress.org/support/users/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)

 *  Plugin Author [Chouby](https://wordpress.org/support/users/chouby/)
 * (@chouby)
 * [5 years, 4 months ago](https://wordpress.org/support/topic/ajaxprefilter-data-should-be-string/#post-14008242)
 * Hello,
 * I move the issue to GitHub: [https://github.com/polylang/polylang/issues/744](https://github.com/polylang/polylang/issues/744)

Viewing 1 replies (of 1 total)

The topic ‘$.ajaxPrefilter .data should be string’ is closed to new replies.

 * ![](https://ps.w.org/polylang/assets/icon-256x256.png?rev=3433336)
 * [Polylang](https://wordpress.org/plugins/polylang/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/polylang/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/polylang/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Chouby](https://wordpress.org/support/users/chouby/)
 * Last activity: [5 years, 4 months ago](https://wordpress.org/support/topic/ajaxprefilter-data-should-be-string/#post-14008242)
 * Status: not resolved