duanche4578 2019-04-20 15:19
浏览 58

Wordpress - 在php更新后,Ajax请求不再有效

After upgrading PHP from 5.6 to 7.2 my ajax request no longer works and throws error 400 Bad Request.

At first I thought it was a file permission error because my code worked fine on my localhost but when I pushed it up to my demo server it failed the ajax request. Several wasted hours of my life later I noticed my localhost was running PHP 5.6. I updated and behold the same error I was seeing on the demo server presented itself.

My main question is: what is different about PHP 7.2 in terms of ajax requests that my code from 5.6 no longer works?

The PHP form request:

                <div class="search-bar">
                    <form action="<?php echo site_url(); ?>/wp-admin/admin-ajax.php" method="GET" id="staff-filter">

                        <div class="input-set">
                            <label>keyword</label>
                            <input type="text" name="search" id="search" value="" placeholder="I'm looking for..." />
                        </div>

                        <?php if( $terms = get_terms( array(
                                                        'taxonomy' => 'department',
                                                        'orderby'  => 'name',
                                                    ) ) ) :

                        echo '<div class="input-set">';
                            echo '<label>department</label>';
                            echo '<select name="department-filter"><option value=" "></option>';
                            foreach( $terms as $term ) :
                                echo '<option value="'. $term->term_id .'">' . $term->name . '</option>';
                            endforeach;
                            echo '</select>';
                        echo '</div>';

                        endif;?>

                        <a class="btn" id="apply-staff-filter" href="">Go</a>

                        <input type="hidden" name="action" value="stafffilter" />
                    </form>
                </div>

                <div class="search-results" id="staff-response">
                </div>

The response:

function filter_staff() {
    $args = array(
        'orderby'   => 'name',
        'post_type' => 'staff',
    );

    if( isset( $_GET['department-filter'] ) ) :
        $args['tax_query'] = array(
            array(
                'taxonomy' => 'department',
                'field'    => 'id',
                'terms'    => $_GET['department-filter']
            )
        );
    endif;

    if( isset( $_GET['search'] ) ) :
        $search = sanitize_text_field( $_GET['search']);
        $query = new WP_Query( array(
            'post_type' => 'staff',
            'tax_query' => $args['tax_query'],
            's' => $search,
        ));

    else :

        $query = new WP_Query( $args );

    endif;


    if( $query->have_posts() ) :

        while( $query->have_posts() ) : $query->the_post(); ?>

        <?php $about = get_field('about'); ?>
        <?php $info = get_field('information'); ?>

            <div class="staff-card">
                <div class="staff-top">
                    <img src="<?php echo $about['photo']; ?>" />
                </div>
                <div class="staff-bot">
                    <p><?php echo $query->post->post_title; ?></p>
                    <p><?php echo $info['title']; ?></p>
                    <a href="tel:<?php echo $info['phone']; ?>"><?php echo $info['phone']; ?></a>
                    <a href="mailto:<?php echo $info['email']; ?>"><?php echo $info['email']; ?></a>
                    <a class="staff-bio btn" href="<?php the_permalink(); ?>">BIO</a>
                </div>
            </div>



        <?php endwhile;

        wp_reset_postdata();

    else :

        echo '<p>We could not find what you were looking for.</p>';

    endif;

    die();
}
add_action( 'wp_ajax_stafffilter', 'filter_staff' );
add_action( 'wp_ajax_nopriv_stafffilter', 'filter_staff' );

and the JS:

    function staff_filter() {
        var filter = $('#staff-filter');
        $.ajax({
            url: filter.attr('action'),
            data: filter.serialize(),
            type: filter.attr('method'),
            beforeSend : function(xhr) {
                filter.find('button').text('Searching...');
            },
            success:  function(data) {
                filter.find('button').text('Apply Filter');
                $('#staff-response').html(data);
            }
        });
        return false;
    }
    $( document ).ready(function() {
        staff_filter();
    });
    $('#apply-staff-filter').on('click', function(e) {
        e.preventDefault();
        staff_filter();
    });

    // Prevent Enter Submit on Staff Directory Search
    $('#search').keypress(function(event) {
        if( event.keyCode == 13 ) {
            event.preventDefault();
        }
    });
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
    • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
    • ¥15 lingo18勾选global solver求解使用的算法
    • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
    • ¥20 测距传感器数据手册i2c