I am having very weird problem. My ajax request, returns only the strings without the actual values in it / except where the class ID is, only there it retrieves the postID /. I am not sure where the problem is but here is my code:
JQUERY:
var roomID = jQuery(this).closest('li').attr('id');
jQuery.ajax({
url: '/wp-json/api/hotelRooms',
data: {
'post_id':roomID
},
type: 'POST',
dataType: 'json',
success: function (data) {
jQuery(".rooms-popups-container").empty();
jQuery(".rooms-popups-container").append(data);
console.log(data);
},
error: function hotelRooms(data) {
console.log('Error:', data);
}
});
PHP: / not posting all the code just small part of it
add_action( 'rest_api_init', function (){
register_rest_route( 'api', '/hotelRooms', array(
'methods' => 'POST',
'callback' => 'hotelRooms',
));
});
function hotelRooms($returndata){
$postID = $_POST['post_id'];
$posttitle = get_the_title($postID);
$postcontent = get_post_field('post_content', $postID);
$galleries = get_post_meta( $postID, '_hb_gallery', true );
$returndata = "";
$returndata .= '<div class="hb_single_room room-popup" id="popup-room-'.$postID.'">
<div class="summary entry-summary">
<div class="title">
<h4>
<a href="#">'. $posttitle .'</a>
<img class="close-room-popup" room_id="'.$postID.'" alt="x" src="https://www.micasas.it/wp-content/uploads/2018/03/x.png">
</h4>
</div>';
return $returndata;
}
In POSTMAN it returns the actual values and everything i need -> http://prntscr.com/me517n but in the Jquery returns only part of the information: http://prntscr.com/me51e0
Ok i get it
You are currently sending room-<id>
from your js instead of <id>
that's why your functions are returning empty values. You can use something like the following in hotelRooms()
$postID = explode("room-",$_POST['post_id'])[1];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.