questions

How return array in function() to javascript on wordpress ajax.php with following code

Rate this post

it may seem stupid, but i’m in it for a week, help me …

the “response” in xhr dev-tools chrome does not leave “0”, it never returns the array I need …

Javascript code to get data from wordpress

$(document).on("click", "[data-show-home-list-series]", function () {
                var id = $(this).attr("data-show-home-list-series");
                $("[data-show-home-list-series]").removeClass("active");
                $(this).addClass("active");
                var $list = $("#homeSliderSerieList");
                $.post("wp-admin/admin-ajax.php", { getHomeSliderSeries: id }, function (html) {
                    var data = jQuery.parseJSON(html);
                    if (data.status == "success") {
                        var listing = data.list;
                        var lister = [];
                        for (var i = 0; i < 20; i++) {
                            var row = listing[i];
                            lister.push(seriePoster(row.url, row.rating, row.poster, row.title, row.cat, row.episode));
                        }
                        $list.parent(".itemsList").addClass("fadingOut");
                        setTimeout(function () {
                            $list.html(lister.join(""));
                            $list.trigger("destroy.owl.carousel");
                            createItemSlider();
                            $list.parent(".itemsList").removeClass("fadingOut");
                        }, 200);
                    } else {
                        return false;
                    }
                });
            });

PHP file to return data in array json to javascript show results in html.

wp-admin/admin-ajax.php (theme/inc/core/ajax.php)

function getHomeSliderSeries(){
// i Want see that bellow in xhr response to javascript:    
//{"status":"success","list":{}}

}

add_action( 'wp_ajax_getHomeSliderSeries', 'getHomeSliderSeries' );
add_action( 'wp_ajax_nopriv_getHomeSliderSeries', 'getHomeSliderSeries' );

My language is not very good, i hope you undestand, thanks atentiton!!

 

✔️Solution:

Try ajax callback function in functions.php

function swt_ajax_data() {
  $id = isset($_POST['id']) ? $_POST['id'] : '';

  // Create an associative array for the response.
  $responsedata = array(
   'id'       => $id,
  ); 
  $result = array();
  
  // if need featch the data from the template un comment the bellow five lines and commented the sixth line
  //ob_start();
  //include('templatepatj');
  //$opr_html .= ob_get_contents();
  //ob_get_clean();
  // $result['data'] = $opr_html;


  $result['data'] = $responsedata;// POST array data.
  }
  return wp_send_json_success($result);
  }
}

add_action('wp_ajax_swt_ajax_data', 'swt_ajax_data');
add_action('wp_ajax_nopriv_swt_ajax_data', 'swt_ajax_data');

Localize your script and pass the admin ajax url

// Register the script
  wp_register_script( 'ajax-script', 'path/to/myscript.js' );
   // Localize the script with new data
   $js_array = array(
   'ajaxurl' => admin_url('admin-ajax.php'),
);
wp_localize_script( 'ajax-script', 'swtobj', $js_array );
// Enqueued script with localized data.
wp_enqueue_script( 'ajax-script' );

Try Js as like below.

$(document).on("click", "[data-show-home-list-series]", function () {
  var id = $(this).attr("data-show-home-list-series");
  $("[data-show-home-list-series]").removeClass("active");
  $(this).addClass("active");
  var $list = $("#homeSliderSerieList");

  var data = {
  'action': 'swt_ajax_data', 'id': id
  };
  $.ajax({
    url: swtobj.ajaxurl,
    type: 'POST',
    data: data,
    cache: false,
    dataType: 'json',
    success: function (response, textStatus, jqXHR) {
      var response_data = response.data.data;
      if (response_data != "" || response_data.length != 0) {
        console.log(response_data);
        // write your code here
      } else {
        // write your code here
      }
    },
  });
});

Leave a Reply

Your email address will not be published.

Back to top button