0

Ah yes, the ol' admin-ajax error 400 issue. I have looked here and here and here for a solution and none of those fixed my problem. So before this is marked as a duplicate, please just hear me out.

My admin-ajax link looks like so:

<script type="text/javascript">
   var ajaxurl = "https://full-url.com/wp-admin/admin-ajax.php";
</script>

My AJAX call looks like this:

var data = {
    action: 'get_events'
};

jQuery.ajax({
  url: ajaxurl,
  data: data,
  type: 'POST',
  success: function(data){ 
    console.log(data);
  },
  error: function(xhr, ajaxOptions, thrownError) {
    console.log(xhr);
    console.log(xhr.status);
    console.log(thrownError);
  }
});

And inside my functions.php, I have the following:

add_action('get_events', 'get_events_func');
add_action('wp_ajax_nopriv_get_events', 'get_events_func');

function get_events_func() {

    echo 'please work';
    exit(); 
    
}

This works perfectly fine when I am logged out of the website, but once I am logged in, it throws an error 400. Did I set up that wp_ajax_nopriv_ hook wrong? That's the only thing I can think of.

The error response I am getting is simply:

XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, abort: ƒ, onreadystatechange: ƒ, …}
abort: ƒ ()
onabort: null
onerror: null
onload: null
onloadend: null
onloadstart: null
onprogress: null
onreadystatechange: ƒ ()
ontimeout: null
readyState: 4
response: "0"
responseText: "0"
responseType: ""
responseURL: "https://full-url.com/wp-admin/admin-ajax.php"
responseXML: null
status: 400
statusText: ""
timeout: 0
upload: XMLHttpRequestUpload {onloadstart: null, onprogress: null, onabort: null, onerror: null, onload: null, …}
withCredentials: false
__proto__: XMLHttpRequest

400

undefined

Any help is really appreciated. Thank you.

3
  • 1
    Are you sure you've carefully reviewed those linked answers? Because that add_action('get_events', should be add_action('wp_ajax_get_events', ..
    – Sally CJ
    Commented May 15, 2021 at 0:39
  • @SallyCJ o. m. g. If you put this as answer, I'll mark it as the solution. I've never felt so dumb. Thank you. Commented May 15, 2021 at 5:16
  • It's alright, everyone makes mistakes.. =) And I've posted an answer, but you should, specially if you're just starting out with admin-ajax.php, consider using the REST API instead. It's well-documented and you can do things like retrieving Posts, Pages, CPT posts, custom taxonomy terms, etc. What's more, you'd get a better response body/text (well, at least it's not simply a 0..) when an error is encountered during an API request, so debugging would become easier than with the ol' admin-ajax.php.. :)
    – Sally CJ
    Commented May 15, 2021 at 8:03

1 Answer 1

2

Did I set up that wp_ajax_nopriv_ hook wrong?

No, you did not. But that hook is for logged-out or unregistered users only.

For logged-in users, the hook is wp_ajax_<action>.

So you just need to change the add_action('get_events' in your code to add_action('wp_ajax_get_events', and the error 400 would be gone:

add_action('wp_ajax_get_events', 'get_events_func');        // for logged-in users
add_action('wp_ajax_nopriv_get_events', 'get_events_func'); // for logged-out users

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.