0

The scenario is whenever client press the key, automatically the suggestion should come below the text box, and that suggested value is fetched from the database.

Here i am stucked in a situation where my ajax is running 'error' property properly but unable to go to 'success' property in ajax .

Here is my js:-

jQuery(document).ready(function(){
     jQuery("#field_2").keyup(function(){
     var text = $(this).val();
     if(text === '')
     {
          jQuery("#result").html('');
     }
     else
     {
      jQuery("#result").html('');
      jQuery.ajax({
      url: ajax_object.ajaxurl,
      method:'get',
      data:
      {
          action : 'my_category',
          search :  text
      },
      dataType:'json',
      success:function(data)
      {
          console.log(data);
         jQuery("#result").html(data);

      },
      error:function(xhr)
       {
         alert("Ajax Failed Due To " +xhr.status+ " error.");
       },

         });
       }
   });  
});

and i enqueue this js in theme folder of function.php and it's code is

function my_category_ajax()
{
      wp_register_script('myAjax', get_template_directory_uri().'/js/custom-ajax.js',array('jquery'));
      wp_localize_script( 'myAjax', 'ajax_object', array( 'ajaxurl' =>admin_url( 'admin-ajax.php' ) ) );
      wp_enqueue_script( 'myAjax' );
}
add_action('wp_loaded', 'my_category_ajax');

and finally the function which is fetching the data from database is again kept in function.php of theme folder and it is:-

function category_from_value()
{
    global $wpbp;
    $mySearch = $_GET['search'];
    $result =  $wpbp->get_results(" SELECT *FROM my_table WHERE value LIKE '{$mySearch}%' ");

    if(count($result) > 0 )
    {
    foreach($result as $my_results)
    {
        $my_results = $result->value;
    }
    echo json_encode($my_results);
    }
     die();
}
add_action('wp_ajax_my_category', 'category_from_value');
add_action('wp_ajax_nopriv_my_category', 'category_from_value');

I can't understand where i am going wrong, any valuable comment is highly appreciable. Thanx

  • 1
    what is the xhr error you are getting back? – mrben522 Mar 25 at 14:14
  • 1
    You're returning an empty response in the no results case, not an empty JSON array, and it looks like jQuery treats that as an error. – Rup Mar 25 at 14:21
  • @mrben522, I am getting error 'xhr.status'= 500 – San Cube Mar 25 at 15:35
  • 500 means 'server error'. Can you see anything in the server's PHP logs? Do you get anything useful if you you try the ajax request manually? – Rup Mar 25 at 15:42
  • Hello there. If you get 500 error, there is a problem with your code and you need to investigate what is wrong. try to add ini_set('display_errors', 1); ini_set('display_startup_errors', 1); and error_reporting(E_ALL); before your function and try to search. You will get an error message from PHP and Server in return. After that, we may solve your problem. – Serkan Algur Mar 29 at 13:01

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.