“Ajax result not returning data” 的中文翻译是:”Ajax 结果未返回数据”。

huangapple go评论57阅读模式
英文:

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:

&lt;form data-ajax-url=&quot;&lt;?php echo admin_url(&#39;admin-ajax.php&#39;); ?&gt;&quot; action=&quot;&lt;?php echo admin_url(&#39;admin-ajax.php&#39;); ?&gt;&quot; method=&quot;POST&quot; id=&quot;lot-filter&quot; class=&quot;lot-form&quot;&gt;
    &lt;fieldset&gt;
    &lt;?php
            if( $terms = get_terms(array(
                &#39;taxonomy&#39; =&gt; &#39;neighborhood&#39;
            )) ) :
                echo &#39;&lt;select class=&quot;input--select&quot; id=&quot;choose-neighborhood&quot; name=&quot;neighborhoodfilter&quot;&gt;&#39;;
                echo &#39;&lt;option value=&quot;&quot;&gt;&#39; . &#39;All Communities&#39; . &#39;&lt;/option&gt;&#39;;
                foreach ( $terms as $term ) :

                    echo &#39;&lt;option value=&quot;&#39; . $term-&gt;term_id . &#39;&quot;&#39; . (($term-&gt;term_id == &#39;1&#39;) ? &#39;hidden&#39; : &#39;&#39;) . ($_POST[&#39;neighborhoodfilter&#39;] == $term-&gt;term_id ? &#39;selected&#39; : &#39;&#39; ) . &#39;&gt;&#39; . $term-&gt;name . &#39;&lt;/option&gt;&#39;; // ID of the category as the value of an option
                endforeach;
                echo &#39;&lt;/select&gt;&#39;;
            endif;
            ?&gt;

        &lt;button&gt;Find Lots&lt;/button&gt;
        &lt;input type=&quot;hidden&quot; name=&quot;action&quot; value=&quot;lot_filter&quot;&gt;
    &lt;/fieldset&gt;
&lt;/form&gt;

My ajax file included in my functions file:

&lt;?php
add_action(&#39;wp_ajax_lot_filter&#39;, &#39;lot_filter_action&#39;); // wp_ajax_{ACTION HERE}
add_action(&#39;wp_ajax_nopriv_lot_filter&#39;, &#39;lot_filter_action&#39;);


function lot_filter_action()
{

    print($_POST);

    die();
}

And my JS(jquery) file:

(function ($) {
    $(document).ready(function () {
        let filter = $(&quot;#lot-filter&quot;);
        let data = filter.serialize();

        function submitFormFilter(e) {
            e.preventDefault();
            console.log(data);
            $.ajax({
                url: filter.data(&#39;ajaxUrl&#39;),
                type: filter.attr(&#39;method&#39;),
                data: data,
                success: function (result) {
                    console.log(result);
                    $(&#39;.response&#39;).html(result);
                },
                error: function (result) {
                    console.warn(result);
                }
            })
        }

        //Lot Search
        $(&#39;#lot-filter&#39;).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 = $(&quot;#lot-filter&quot;);

    function submitFormFilter(e) {
       //move this line here
       let data = filter.serialize();
        e.preventDefault();
        console.log(data);
        $.ajax({
            type: &#39;post&#39; ,
            url: filter.data(&#39;ajaxUrl&#39;),
            data: data,
            success: function (result) {
                console.log(result);
                $(&#39;.response&#39;).html(result);
            },
            error: function (result) {
                console.warn(result);
            }
        })
    }

    //Lot Search
    $(&#39;#lot-filter&#39;).submit(submitFormFilter);


});
})(jQuery);

if you need to send data to js file shoude use "wp_localize_script "

huangapple
  • 本文由 发表于 2023年7月4日 21:58:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613400.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定