我试图通过Ajax调用获取ACF字段的内容。这是我的回调函数:
function my_ajax_request() {
$id = $_POST[\'id\'];
if( have_rows(\'carousel_slide\') ):
// Loop through rows
while( have_rows(\'carousel_slide\') ) : the_row();
// Load sub Fields
$cta_text = get_sub_field(\'cta_text\');
// Allowed html
$allowed_html = array(
\'strong\' => array()
);
?>
<div class="cta-popup">
<!-- Modal -->
<div class="modal fade" id="cta-modal-1<?php echo $id; ?>" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<p><?php echo wp_kses( $cta_text, $allowed_html );?></p>
</div>
</div>
</div>
</div>
</div>
<?php
// End loop
endwhile;
// End if
endif; ?>
</div><!-- end carousel-inner -->
<?php
die();
}
add_action( \'wp_ajax_my_ajax_request\', \'my_ajax_request\' );
这是我的Ajax调用:
jQuery(\'.btn\').click(function () {
var id = jQuery(this).attr(\'data-target\').replace(\'#\', \'\');
console.log(id);
jQuery.ajax({
url: ajaxurl.url,
type: \'POST\',
data: {
action: \'my_ajax_request\',
id : id
},
success: function (result) {
jQuery(\'.modal\').attr(\'id\',id);
jQuery(id).modal(\'show\');
alert(id);
},
error: function () {
alert("error");
}
});
});
});
id来自一个按钮,单击该按钮应打开引导模式。然而,我不断收到以下通知:
Notice: Undefined index: my_id in /Applications/MAMP/htdocs/wordpress/wp-content/themes/my-teme/functions.php on line 61
想法是在我的\\u Ajax\\u请求()上使用Ajax调用的id。我错过了什么?