将PHP从5.6升级到7.2后,我的ajax请求不再起作用,并引发错误400 Bad Request。
起初,我认为这是文件权限错误,因为我的代码在localhost上可以正常运行,但是当我将其推送到演示服务器时,ajax请求失败。我一生中浪费了几个小时,然后才发现本地主机运行的是PHP 5.6。我更新并看到了在演示服务器上看到的相同错误。
我的主要问题是:就ajax请求而言,我的5.6版本的代码不再有效,PHP 7.2有何不同?
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();
}
});