英文:
Ajax result not returning data
问题
这是你的翻译:
我有一个WP项目,我正在尝试进行一些Ajax筛选,但我在初始Ajax调用中遇到了一些问题,无法返回任何数据。我有一列自定义文章类型的文章,我想要能够对它们进行筛选(可以按分类或ACF字段)。目前我只是尝试使一个筛选器正常工作。然而,没有返回任何数据。你有任何想法我做错了什么吗?
这是我的表单:
<form data-ajax-url="<?php echo admin_url('admin-ajax.php'); ?>" action="<?php echo admin_url('admin-ajax.php'); ?>" method="POST" id="lot-filter" class="lot-form">
<fieldset>
<?php
if( $terms = get_terms(array(
'taxonomy' => 'neighborhood'
)) ) :
echo '<select class="input--select" id="choose-neighborhood" name="neighborhoodfilter">';
echo '<option value="">' . 'All Communities' . '</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '"' . (($term->term_id == '1') ? 'hidden' : '') . ($_POST['neighborhoodfilter'] == $term->term_id ? 'selected' : '' ) . '>' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<button>Find Lots</button>
<input type="hidden" name="action" value="lot_filter">
</fieldset>
</form>
我的Ajax文件包含在我的函数文件中:
<?php
add_action('wp_ajax_lot_filter', 'lot_filter_action'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_lot_filter', 'lot_filter_action');
function lot_filter_action()
{
print($_POST);
die();
}
以及我的JS(jQuery)文件:
(function ($) {
$(document).ready(function () {
let filter = $("#lot-filter");
let data = filter.serialize();
function submitFormFilter(e) {
e.preventDefault();
console.log(data);
$.ajax({
url: filter.data('ajaxUrl'),
type: filter.attr('method'),
data: data,
success: function (result) {
console.log(result);
$('.response').html(result);
},
error: function (result) {
console.warn(result);
}
})
}
//Lot Search
$('#lot-filter').submit(submitFormFilter);
});
})(jQuery);
一旦我让它工作,我将通过Ajax文件显示结果,但在那之前,我只是尝试查看返回了什么。我的分类法有两个社区,我期望看到返回一些'riverview'或'cherokee'的文章,但现在只是得到一个空数组($_POST)。
英文:
I have a WP project where I'm trying to do some ajax filtering but I'm having some trouble returning any data from my initial ajax call. I have a list of posts that are custom post types, and I'd to be able to filter them out (either by taxonomy, or and ACF field). I'm currently just trying to make one filter work correctly. No data is being returned though. Any idea what I'm doing wrong???
Here is my form:
<form data-ajax-url="<?php echo admin_url('admin-ajax.php'); ?>" action="<?php echo admin_url('admin-ajax.php'); ?>" method="POST" id="lot-filter" class="lot-form">
<fieldset>
<?php
if( $terms = get_terms(array(
'taxonomy' => 'neighborhood'
)) ) :
echo '<select class="input--select" id="choose-neighborhood" name="neighborhoodfilter">';
echo '<option value="">' . 'All Communities' . '</option>';
foreach ( $terms as $term ) :
echo '<option value="' . $term->term_id . '"' . (($term->term_id == '1') ? 'hidden' : '') . ($_POST['neighborhoodfilter'] == $term->term_id ? 'selected' : '' ) . '>' . $term->name . '</option>'; // ID of the category as the value of an option
endforeach;
echo '</select>';
endif;
?>
<button>Find Lots</button>
<input type="hidden" name="action" value="lot_filter">
</fieldset>
</form>
My ajax file included in my functions file:
<?php
add_action('wp_ajax_lot_filter', 'lot_filter_action'); // wp_ajax_{ACTION HERE}
add_action('wp_ajax_nopriv_lot_filter', 'lot_filter_action');
function lot_filter_action()
{
print($_POST);
die();
}
And my JS(jquery) file:
(function ($) {
$(document).ready(function () {
let filter = $("#lot-filter");
let data = filter.serialize();
function submitFormFilter(e) {
e.preventDefault();
console.log(data);
$.ajax({
url: filter.data('ajaxUrl'),
type: filter.attr('method'),
data: data,
success: function (result) {
console.log(result);
$('.response').html(result);
},
error: function (result) {
console.warn(result);
}
})
}
//Lot Search
$('#lot-filter').submit(submitFormFilter);
});
})(jQuery);
Once I get it working I will display the results via the ajax file, but until then I'm simply trying to see what being returned. My taxonomy has two neighborhoods. I'm expecting to see either some 'riverview' or 'cherokee' posts returned, but now just getting an empty array ($_POST).
答案1
得分: 1
我看到了你的代码,只有在表单点击后才使用 "filter.serialize" 函数来获取所有数据,代码如下:
(function ($) {
$(document).ready(function () {
let filter = $("#lot-filter");
function submitFormFilter(e) {
//将这行代码移到这里
let data = filter.serialize();
e.preventDefault();
console.log(data);
$.ajax({
type: 'post',
url: filter.data('ajaxUrl'),
data: data,
success: function (result) {
console.log(result);
$('.response').html(result);
},
error: function (result) {
console.warn(result);
}
})
}
//Lot Search
$('#lot-filter').submit(submitFormFilter);
});
})(jQuery);
如果需要将数据发送到 JavaScript 文件中,应使用 "wp_localize_script"。
英文:
I saw your codes
only use "filter.serialize" function after form click to get all data like this :
(function ($) {
$(document).ready(function () {
let filter = $("#lot-filter");
function submitFormFilter(e) {
//move this line here
let data = filter.serialize();
e.preventDefault();
console.log(data);
$.ajax({
type: 'post' ,
url: filter.data('ajaxUrl'),
data: data,
success: function (result) {
console.log(result);
$('.response').html(result);
},
error: function (result) {
console.warn(result);
}
})
}
//Lot Search
$('#lot-filter').submit(submitFormFilter);
});
})(jQuery);
if you need to send data to js file shoude use "wp_localize_script "
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论