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!!
2
Answers
The data you send in
$.post
is missing anaction
property. This property is responsible for telling theadmin-ajax.php
which function that is registered with awp_ajax_{function_name}
hook should be called.The value in the
action
property should match thefunction_name
inwp_ajax_{function_name}
andwp_ajax_nopriv_{function_name}
to be called properly.Combine the
action
property with other properties that you want to send to the backend to pass the data that you need to send.Now on the server side your
getHomeSliderSeries
should be called. All the other properties (action included) can now be accessed through the global$_POST
variable with their corresponding keys.For a response, create an associative array with the data you want to return. This is the equivalent of an object in JavaScript. Encode the array to JSON and send it back. Now the frontend should see an object as a response.
Try ajax callback function in functions.php
Localize your script and pass the admin ajax url
Try Js as like below.