I have a button with data :
data-boutique-id="<?php echo esc_attr(get_the_ID());
In my main.js i have 2 jQuery functions using those 2 variables :
$fav = $('.fav-btn'),
boutiqueId = $fav.data('boutique-id');
My on('click')
function is working fine, it does return the data-boutique-id
. Bu my ready()
function is not working and the variable boutiqueId
is returning [object Object]
instead of the Id. There must be something i don't know.
var $fav = $('.fav-btn'),
boutiqueId = $fav.data('boutique-id');
$fav.ready(function(theBoutique) {
$.ajax({
type: 'GET',
data: {
action: 'check-fav',
boutiqueId: theBoutique
},
url: '/wp-json/fav_ajax/v1/manage_fav/'
}).done(function(_data) {
console.log(_data);
});
});
function manage_fav_rest_route()
{
// Path to ajax
register_rest_route(
'fav_ajax/v1',
'/manage_fav/',
array(
'methods' => WP_REST_SERVER::READABLE,
'callback' => 'handle_rest_call'
)
);
}
function handle_rest_call()
{
$the_boutique = $_GET['boutiqueId'];
$the_action = $_GET['action'];
if ('check-fav' == $the_action)
{
return $the_boutique;
}
}