【问题标题】:Wordpress: Ajax request logs 400 Bad Request to consoleWordpress:Ajax 请求向控制台记录 400 Bad Request
【发布时间】:2019-09-22 08:04:13
【问题描述】:

我不断收到此错误:

POST http://localhost/wptest2/wp-admin/admin-ajax.php 400 (Bad Request)
(anonymous) @ VM12565:1
send @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
ajax @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
(anonymous) @ options-general.php?page=fvc-settings:784
dispatch @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
r.handle @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
  1. 在下面的代码中,ajaxPost() 与我的管理页脚挂钩 插入。
  2. 单击 jQuery 选择的按钮时,它应该触发已注册的 post 请求并返回一个简单的响应。

这不是 jQuery.post() 或 $.ajax() 所发生的。我在 cmets 中尝试了下面提到的几件事,但无法正常工作,谁能告诉我这个问题?

function ajaxPost() {
  ?>

  <script>     
    jQuery(document).ready(function($) {        
      $('#previousButton').click(function() {
        var postData = {
          action : 'selectTargetsWithOffset',
          data : {
            offsetMultiplier : 1,
            incrementSize : 10
          },
          success : function(response) {
            console.log(`=====jQuery success=====`);
          }
        };

        var handleResponse = function(response) {
          console.log(response, `=====response=====`);
        };

        jQuery.post(ajaxurl, postData, handleResponse);

        // this code also threw a 400 error when 
        // I commented out the above code instead
        // $.ajax({
        //   type: "POST",
        //   url: ajaxurl,
        //   dataType: "json",
        //   data: {
        //     action: "selectTargetsWithOffset",
        //   },
        //   success: function() {
        //     console.log(response, `=====response 
        //       from $.ajax}=====`);
        //   }
        // });

      })

    })
  </script>
  <?php
}
add_action('admin_footer', 'ajaxPost');


function selectTargetsWithOffset() {
  wp_send_json([ 'message' => 'from back end' ]);
  wp_die();
}
add_action('wp_ajax_selectTargetsWithOffset', 'selectTargetsWithOffset');

也试过了 - 进入 admin-ajax.php 并将响应代码从 400 更改为 420 以查看它是否会执行任何操作。我确实收到了一些反映更改的奇怪错误。

if ( is_user_logged_in() ) {
// If no action is registered, return a Bad Request response.
if ( ! has_action( "wp_ajax_{$action}" ) ) {
    wp_die( 'Test', 420 );
}

在 admin-ajax.php 的顶部添加以下内容:

  ?>
    <script>
      console.log(`=====TEST=====`);
      var request = "<?php echo $_REQUEST ?>";
      console.log(request['action'], `=====request=====`);
    </script>
  <?php

当我尝试在我的管理区域触发 ajax 调用时,我仍然只收到 400 bad response 控制台日志。但是,当我直接导航到 URL 时,现在我看到 ===TEST=== 语句并且请求被标记为未定义(如我所料)。我认为这意味着我如何调用 ajax 请求是错误的。

这是我调用按钮的 HTML 代码:

<button id="previousButton">Previous</button>

响应简化回调建议

这是控制台日志:

=====jQuery success=====
VM13659:1 POST http://localhost/wptest2/wp-admin/admin-ajax.php 400 (Bad Request)
(anonymous) @ VM13659:1
send @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
ajax @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
n.(anonymous function) @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:4
(anonymous) @ options-general.php?page=fvc-settings:785
dispatch @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3
r.handle @ load-scripts.php?c=0&load[]=jquery-core,jquery-migrate,utils&ver=5.1.1:3

这里是代码:

function ajaxPost() {
  ?>
  <script>

    jQuery(document).ready(function() {
      jQuery('#previousButton').click(function() {

        var postData = {
          action : 'selectTargetsWithOffset',
          data : {
            offsetMultiplier : 1,
            incrementSize : 10
          },
          success : function(response) {
            console.log(`=====jQuery success=====`);
          }
        };

        var handleResponse = function(response) {
          console.log(response, `=====response=====`);
        };

        jQuery.post(ajaxurl, postData, handleResponse);
      }); 
    })
  </script>
  <?php
}
add_action('admin_footer', 'ajaxPost');


function selectTargetsWithOffsetcb() {
  print_r($_POST);
  die();
}

add_action( 'wp_ajax_selectTargetsWithOffset', "selectTargetsWithOffsetcb");
add_action( 'wp_ajax_nopriv_selectTargetsWithOffset', "selectTargetsWithOffsetcb" );

页面本身没有 print_r() 输出,它保持不变,无需任何刷新

再次值得注意的是,admin-ajax.php 中的调试代码====TEST=== 未能登录。我认为这表明请求由于某种原因没有到达该文件(因为在浏览器中导航到该文件确实会导致它登录到控制台)


这个也试过了,也是400错误:

function ajaxPost() {
  ?>
  <script>    
    jQuery(document).ready(function() {
     jQuery(document).on("click","#previousButton", function() {
       jQuery.ajax({
         url: "<?php echo admin_url( 'admin-ajax.php' ) ?>",
         data: {"action":"selectTargetsWithOffset","datas": {"name": "yourfields"}},
         type:"post",
         success: function(data) { alert(data); }
       })
     });    
    })
  </script>
  <?php
}
add_action('admin_footer', 'ajaxPost');

【问题讨论】:

  • 在您的第二个 AJAX 代码中,尝试将 "action" 更改为简单的 action(无双引号):action: "selectTargetsWithOffset"
  • @cabrerahector 用于对象,键可以用单引号、双引号或不引号括起来。我注意到的一件主要事情是,你没有在哪里声明 ajaxurl
  • ajaxurl 变量已在 WordPress 的管理部分中可用,因此 OP 不需要声明它。
  • 好的,我试过了,但不幸的是它没有改变任何东西

标签: jquery ajax wordpress


【解决方案1】:

我认为你没有在你的插件或函数文件中声明回调函数。

请将以下代码添加到您的插件或 fn 文件中并进行测试。

add_action( 'wp_ajax_selectTargetsWithOffset', "selectTargetsWithOffsetcb");
add_action( 'wp_ajax_nopriv_selectTargetsWithOffset', "selectTargetsWithOffsetcb" );

function selectTargetsWithOffsetcb()
{

    print_r($_POST);
    die();
}

也更新这个js

    <script>
jQuery(document).on("click","#previousButton",function(){

jQuery.ajax({
url:"<?php echo admin_url( 'admin-ajax.php' ) ?>",
data:{"action":"selectTargetsWithOffset","datas":{"name":"yourfields"}},
type:"post",
success:function(data){alert(data);}
})
});
</script>

【讨论】:

  • 谢谢,我已经在原帖中发布了我的尝试
  • 你看到上面了吗?不幸的是,它仍然无法正常工作
  • 你是如何测试它的?你在哪里写了这段代码?你能提供任何视频网址吗?
  • 我将修改后的文件保存在我的 IDE 中,然后强制刷新浏览器。由于站点位于 /var/html/www 它代理到 localhost/
猜你喜欢
  • 1970-01-01
  • 2020-05-29
  • 2013-10-02
  • 2019-10-22
  • 2021-07-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多