Luke Clynes
Luke Clynes

Reputation: 195

wp_ajax returning 400 Bad Request

Good morning,

Sorry if this has been posted before. My reason for posting another is that none of the solutions on the other posts has resolved my issue.

Whenever I post to my admin-ajax.php URL, I get a 400 Bad Request response. I've been trying to make this work for ages now.

Here's the PHP end of the code:

<?php

  if (!defined('ABSPATH')) exit;

  define("PLUGIN_VERSION", '0.0.02');

  add_action('wp_enqueue_scripts', 'findajob_load_scripts');
  function findajob_load_scripts() {
      global $wp;
    $current_page = home_url(add_query_arg(array(), $wp->request));
    wp_register_style('findajob-css', plugins_url('/findajob.css', __FILE__), array(), PLUGIN_VERSION);
    wp_register_script('findajob-js', plugins_url('/findajob.js', __FILE__), array('jquery'), PLUGIN_VERSION, true);
      wp_localize_script('findajob-js', 'findjob', array('ajaxurl' => admin_url('admin-ajax.php'), 'current_page' => $current_page));
  }

  add_shortcode('findajob-form', 'findajob_shortcode');
  function findajob_shortcode($atts, $content = null) {
      global $wp;
      $current_page = home_url(add_query_arg(array(), $wp->request));
      wp_enqueue_style("findajob-css");
      wp_enqueue_script("findajob-js");
      ?>
      <form type="POST">
          <button name="Test">Click me!</button>
      </form>
      <?php
  }

  add_action("wp_ajax_findajob_admin_ajax", "findajob_admin_ajax");
  add_action("wp_ajax_nopriv_findajob_admin_ajax", "findajob_admin_ajax");
  function findajob_admin_ajax() {
      global $wpdb;
      echo "testing";
      wp_die();
  }

?>

And here is the ajax request in JS:

$(document).ready(function($){
  $('button[name="Test"]').on('click', function(e){
        e.preventDefault();
        $.ajax({
            url: findjob.ajaxurl,
            type: 'POST',
            // contentType: 'text',
            data: {
                'action': "send_form"
            },
            success: function(Resp) {
                alert(Resp);
            },
            error: function(a, b, c) {
                console.log(a, b, c);
            }
        });
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

I'm hoping that somebody can tell me what I'm doing wrong.

Thanks!

Upvotes: 1

Views: 49

Answers (1)

Titus
Titus

Reputation: 806

The action paramter inside your ajax call has to be the name of your Ajax action.

add_action("wp_ajax_findajob_admin_ajax",....

you have to name your action as:

$(document).ready(function($){
  $('button[name="Test"]').on('click', function(e){
        e.preventDefault();
        $.ajax({
            url: findjob.ajaxurl,
            type: 'POST',
            // contentType: 'text',
            data: {
                'action': "findajob_admin_ajax"
            },
            success: function(Resp) {
                alert(Resp);
            },
            error: function(a, b, c) {
                console.log(a, b, c);
            }
        });
    });
});

Upvotes: 2

Related Questions