I am using the Code Snippets Plugin for WordPress to send a jQuery Ajax request but I keep receiving a

POST https://example.com/wp-admin/admin-ajax.php 400 (Bad Request)

I have tried what seems to be a thousand ways to formulate the Ajax request following examples from stackoverflow, jQuery forums, WordPress forums, and it goes on. Always with the same error.

Here is what I currently have in my Snippet editor:

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' ) ?>';

        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
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            console.log("we failed again: " + JSON.stringify(XMLHttpRequest));
            console.log("text status: " + textStatus);
            console.log("errorThrown: " + errorThrown)

Then my php handler inside the same snippet editor:

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);
add_action('wp_ajax_retrieveAttendees', 'retrieveAttendees_request');
add_action('wp_ajax_nopriv_retrieveAttendees', 'retrieveAttendees_request');

I have tried adding the dataType to the Ajax, no luck. Tried adding both dataType and ContentType, no luck. I have no clue as to why I keep getting the same POST 400 Bad Request error. I would appreciate any help and insight.

  • Where is your PHP code? In a snippet? Jul 11, 2021 at 4:56
  • Yes, I was trying to use just one snippet to have both JavaScript and php code in it. I wanted to avoid making a plugin altogether.
    – Gerardo C.
    Jul 13, 2021 at 20:53
  • I ended up making a plugin and enqueing my scripts and had no problems with the Ajax request. Would still like to know why using the Snippets Plugin failed.
    – Gerardo C.
    Jul 13, 2021 at 20:55

1 Answer 1


You'll need to look at the server side logs to see what's actually causing the 400. Without this, it's just conjecture. With it, you should see exactly what's causing the problem.

However, I conjecture that you're lacking authentication headers, or an authentication request. I'm not seeing anything related to authentication in your AJAX request, and anything POST'ing to admin-ajax.php probably should prove it's an admin first. Take a look at this:

How do I correctly setup an AJAX nonce for WordPress REST API? . You'll probably need to adjust it a bit, since it looks like they're using a public endpoint that wants user authentication, while you're using an admin endpoint, but in either case you need some sort of authentication (by default.)

  • I'll try using the Ajax nonce and see if it works with that. Thanks for the tip
    – Gerardo C.
    Jul 10, 2021 at 19:55
  • You're welcome. I'd still try and look at the error logs first, if I were you. If you don't have detailed enough error reporting, you'll spend forever on issues. The error log might say something like "missing authentication header" on line 37 that would save you a lot of time. Jul 11, 2021 at 21:26
  • Would it be the web server's general web logs?
    – Gerardo C.
    Jul 13, 2021 at 20:54
  • Probably - I'd create an error.php file in your WordPress root with an error in it, and then go to mysite.com/error.php, and see how you can view the resulting error in your error log. You'll use the same process to debug your AJAX request. Jul 14, 2021 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.