我正在使用WordPress的代码片段插件发送jQuery Ajax请求,但我一直收到
发布https://example.com/wp-admin/admin-ajax.php 400(错误请求)
我已经尝试了似乎有一千种方法来制定Ajax请求,下面是stackoverflow、jQuery论坛、WordPress论坛中的示例,并且还在继续。总是有相同的错误。
以下是我当前在代码段编辑器中拥有的内容:
JavaScript code
var jq = jQuery.noConflict();
// a bunch of code to determine what day is selected on a calendar, this all works up until the ajax call
jq(".c-day-content").click(function () {
console.log ("handled the day being picked quite nicely...");
day = jq(this).text();
day = day.replace(/[^0-9]/g,\'\');
console.log("day=" + day);
bookingStart = bookingStart.concat(day);
//query the db looking for time slots with appointments and returning this array
var ajaxurl = \'<?php echo admin_url( \'admin-ajax.php\' ) ?>\';
console.log(ajaxurl);
jq.ajax({
url: ajaxurl,
data: {
action: "retrieveAttendees",
dateSelected: bookingStart //bookingStart is variable that has a string value
},
method: \'POST\',
success: function(data, XMLHttpRequest) {
//never been able to make it to here
console.log(JSON.stringify(data));
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log("we failed again: " + JSON.stringify(XMLHttpRequest));
console.log("text status: " + textStatus);
console.log("errorThrown: " + errorThrown)
}
});
然后我的
php 同一代码段编辑器中的处理程序:
function retrieveAttendees_request() {
console.log("made it to the ajax request"); // I\'ve never made it here either
if ( isset($_REQUEST)) {
$bookingDay = $_REQUEST[\'dateSelected\'];
global $wpdb;
$sql = $wpdb->prepare("select apps.bookingStart, COUNT(*) as booked from wp_amelia_customer_bookings as books inner join wp_amelia_appointments as apps on books.appointmentId = apps.id where apps.bookingStart like %s\'%\' and apps.status = \'approved\' GROUP BY books.appointmentId;", $bookingDay);
$result = $wpdb->get_row($sql, ARRAY_A);
echo json_encode($result);
}
die();
}
add_action(\'wp_ajax_retrieveAttendees\', \'retrieveAttendees_request\');
add_action(\'wp_ajax_nopriv_retrieveAttendees\', \'retrieveAttendees_request\');
我尝试将数据类型添加到Ajax中,但没有成功。尝试同时添加数据类型和内容类型,但没有成功。我不知道为什么我总是收到同样的POST 400错误请求错误。如果有任何帮助和见解,我将不胜感激。