【发布时间】:2019-01-30 09:08:41
【问题描述】:
我遇到了非常奇怪的问题。我的 ajax 请求仅返回其中没有实际值的字符串 / 除了类 ID 所在的位置,它仅在那里检索 postID /。 我不确定问题出在哪里,但这是我的代码:
查询:
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: / 没有发布所有代码,只是其中的一小部分
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;
}
在 POSTMAN 中它返回实际值和我需要的一切 -> http://prntscr.com/me517n 但在 Jquery 中只返回部分信息:http://prntscr.com/me51e0
【问题讨论】:
-
您的
$posttitle, $postcontent, ...好像是空的,get_the_title(), ...函数是如何工作的? -
@RomainB。如果他们是空的,他们就不会在邮递员身上工作,对吗? get_the_title 按 ID 获取标题,我从 JS 中的变量 roomID 中获得。
-
@milen 你检查过你是否真的有 JS 中的 roomID。由于 PHP 代码通过 Postman 可以正常工作,因此很可能是 JS。
-
@BilalAkbar 是的,prntscr.com/me51e0 从这个屏幕截图中你可以看到它得到了正确的产品 ID/当点击不同的链接时它会改变我测试了它/
-
@milen 您应该首先尝试在您的 php 中调用
get_the_title()和其他具有$postID静态值的函数(例如 4464),然后开始调试您的函数。可能你的电话和 Postman 有点不一样