0

I have a select dropdown where users can choose multiple options that get's sent to a custom Rest-API endpoint and returns values based on their selection(s).

The dropdown is dynamically populated and can contain up to 422 options depending on previous logic. When I test it I can choose up to 300 options and the AJAX call will still be sucessfull. But if I choose all of the 422 options and try to send that array to the custom endpoint, it returns a 400 bad request error. I am not new to the world of Rest-API and AJAX but I'm new to the practice of sending such a large array of values, so I don't really know why this is happening in the first place. I send the same array, it's just the length of the array that causes the issue. Need a helpful answer on this one.

Here is the AJAX call:

getTFEk(getTCU) {

              jQuery('.loaderImage').show();

              var kValue = $("#filterKValues").val();
              var kValues = kValue.map(Number);

              jQuery.ajax({
                beforeSend: (xhr) => {
                  xhr.setRequestHeader('X-WP-Nonce', ***.nonce);
                },
                url: ***.root_url + '/wp-json/create/v1/manageTCU',
                type: 'GET',
                dataType: 'html',
                data: {
                        'koValue' : kValues,
                },
                  success: (response) => {

                    jQuery('.loaderImage').hide();

                    console.log(response)

                  },

                  error: (response) => {

                    jQuery('.loaderImage').hide();

                    console.log(response)

                  } 
              });

            }

Here is the custom Rest-API endpoint callback function:

function getAvailTCU ($data) {

            $koValue     = $data['koValue'];

            //First WP_Query arguments
            $taxonomy_args = array(
              'post_type' => 'gl_tcu_offer',
              'tax_query' => array(
                array(
                  'taxonomy' => 'gl_tcu_tax',
                  'field'    => 'id',
                  'terms'    => $koValue,
                ),
              ),
            );


            $wp_query_taxonomy = new WP_Query( $taxonomy_args );

            $ko_posts = $wp_query_taxonomy->posts;

            $ko_posts_ids = join(', ', wp_list_pluck( $ko_posts, 'ID' ));


            $term_object_args = array(
              'fields' => 'ids',
            );

            $tcu_terms_in_use = wp_get_object_terms( $ko_posts_ids, 'gl_tcu_tax', $term_object_args );  

              if( $terms = get_terms( array(
                  'taxonomy' => 'gl_tcu_tax',
                  'exclude' => $tcu_terms_in_use,
                  'hide_empty' => false
              ) ) ) : 

                echo '<option selected disabled>Chose TCU...</option>';
                foreach ( $terms as $term ) :
                  echo '<option value="' . $term->slug . '">' . $term->name . '</option>'; // ID of the category as an option value
                endforeach;
             else :
              echo 'No TCU Precent';
            endif;

            die();

            }
  • "400 bad request error" - what was the exact error? – Sally CJ Apr 29 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.