简体   繁体   English

wp_ajax 返回 400 错误请求

[英]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.每当我发布到我的admin-ajax.php URL 时,我都会收到400 Bad Request响应。 I've been trying to make this work for ages now.多年来,我一直在努力完成这项工作。

Here's the PHP end of the code:这是代码的 PHP 结尾:

<?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:这是JS中的ajax请求:

 $(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!谢谢!

The action paramter inside your ajax call has to be the name of your Ajax action. ajax 调用中的动作参数必须是 Ajax 动作的名称。

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);
            }
        });
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM