После обновления PHP с 5.6 до 7.2 мой ajax-запрос больше не работает и выдает ошибку 400 Bad Request.
Сначала я подумал, что это ошибка разрешения файла, потому что мой код работал на моем локальном хосте, но когда яподтолкнул его к моему демонстрационному серверу, он не прошел ajax-запрос.Спустя несколько часов моей жизни я заметил, что мой localhost работает под управлением PHP 5.6.Я обновил, и вот та же самая ошибка, которую я видел на демонстрационном сервере, представила себя.
Мой главный вопрос: что отличается от PHP 7.2 с точки зрения запросов ajax, что мой код из 5.6 больше не работает?
Запрос формы PHP:
<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>
Ответ:
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' );
и 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();
}
});