I am trying to load posts via ajax in WordPress but I am getting admin-ajax.php 400 (Bad Request) error. I have used the same code in one other website and it was working there but here in this website it is not working. I am not able to understand what is wrong with my code. Here is my jquery

var pageNumber_pa_jobs = 1;
      var total_number_of_pages_pa_jobs = jQuery('.total_number_of_pages_pa_jobs').text();
      var button = jQuery(this);
      var url = window.location.protocol + "//" + window.location.host + "/wp-admin/admin-ajax.php";
          type: "POST",
           data: {
               action: 'loadmore_pa_jobs',
               'pageNumber_pa_jobs' : pageNumber_pa_jobs,
               security: '<?php echo $ajax_nonce; ?>'

           beforeSend : function ( xhr ) {
               button.text('Loading...'); // change the button text, you can also add a preloader image

           success: function(data){
               if(pageNumber_pa_jobs == total_number_of_pages_pa_jobs){
               button.text(' Load More PA Jobs '); // change the button text, you can also add a preloader image

Here is my PHP code

function pa_loadmore_ajax_handler_jobs(){
    echo "ajax working";
    die; // here we exit the script and even no wp_reset_query() required!
add_action('wp_ajax_loadmore_pa_jobs', 'pa_loadmore_ajax_handler_jobs'); // wp_ajax_{action}
add_action('wp_ajax_nopriv_loadmore_pa_jobs', 'pa_loadmore_ajax_handler_jobs'); // wp_ajax_nopriv_{action}

I am seeing 400 bad request error in console

POST https://example.com/wp-admin/admin-ajax.php 400 (Bad Request)
send    @   jquery.js?ver=1.12.4:4
ajax    @   jquery.js?ver=1.12.4:4
(anonymous) @   (index):2189
dispatch    @   jquery.js?ver=1.12.4:3
r.handle    @   jquery.js?ver=1.12.4:3

What is wrong with my code. I have spent many hours but was not able to figure out this issue. I will really appreciate your help.

  • Your code works fine for me. In the 400 error in the console are you certain that the URL is correct? – Jacob Peattie Aug 29 '18 at 16:32
  • are you using example.com locally? Or have you just hidden the real domain? Can you indent your code correctly? It's very difficult to read – Tom J Nowell Aug 29 '18 at 16:37
  • Yes I am hundred percent sure that the url is correct. This is a live website and if you need I can share URL with you. – wplearner Aug 29 '18 at 16:40
  • @Awan are you sure that's the exact AJAX endpoint? Where is the PHP located? Plugin? Theme functions? There's a nonce etc being sent yet there are no nonce checks at the other end, just an echo and a die statement. Is there a particular reason you didn't opt for the super/easier REST API endpoints? They're easier to implement and easier to use, and if something goes wrong it usually tells you what went wrong in plain english rather than sending cryptic error codes – Tom J Nowell Aug 29 '18 at 18:23
  • Maybe your add_action() calls are done after WordPress has finished checking for any callbacks hooked to the wp_ajax_loadmore_pa_jobs or wp_ajax_nopriv_loadmore_pa_jobs action. Also, since you have PHP code in your JS script, you could do var url = '<?php echo admin_url( 'admin-ajax.php' ); ?>'; – Sally CJ Aug 30 '18 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.