【问题标题】:Ajax with wordpress, 400 bad request带有wordpress的Ajax,400错误请求
【发布时间】:2020-09-30 09:14:55
【问题描述】:

我尝试使用 Ajax 在插件中提交表单。我开发了两个插件,第一个可以工作,但现在不行了,我没有发现任何错误。我认为这不是来自它自己的代码,但我有点迷茫。

这是我的 php:

wp_enqueue_script( 'b_form', plugin_dir_url( __FILE__ ) . 'js/b-form.js', array( 'jquery') );
wp_localize_script( 'b_form', 'ajax_url', admin_url( 'admin-ajax.php' ) );
include plugin_dir_path( __FILE__ ) . 'pages/b-form.php';

还有我的 js:

var fd = new FormData( document.getElementById( 'b_form' ) );
fd.append( 'action', 'b_generate_comparison' );
$.ajax({
     type: "POST",
     url: ajax_url,
     data: fd,
     contentType: false,
     processData: false,
        })
.done( function( response ) {
...
}
.fail( function( response ) {
...
}
.always( function( response ) {
...
}

【问题讨论】:

  • 服务器端 php 日志中有任何信息吗?
  • 确实,收到了正确的消息错误,谢谢,我没想到会到达那里

标签: javascript php jquery ajax wordpress


【解决方案1】:

尝试下面的代码,如果它不能工作,然后检查以下事项 - 在下面的代码中更改 URL -

var fd = new FormData( document.getElementById( 'b_form' ) );
fd.append( 'action', 'b_generate_comparison' );
$.ajax({
     type : 'POST',
     url : 'http://YOUR_SITE_URL/wp-admin/admin-ajax.php',
     dataType : 'json',
     data: fd,
     contentType: false,
     processData: false,
success : function( response ) {        
.done( function( response ) {
...
}
.fail( function( response ) {
...
}
.always( function( response ) {
...
}
} // success 
 });    // Ajax request end

清单

  1. AJAX URL(对于 wordpress 默认是 your_site_url/wp-admin/admin-ajax.php)
  2. DataType 可以是“json”, 'html', 'xml' 通过你的 请求

希望这会有所帮助。快乐编码。

【讨论】:

    【解决方案2】:

    我正在发布文件,而且文件很大,

    所以在/etc/php/7.0/apache2/php.ini增加

    upload_max_filesize = 13M
    post_max_size = 27M
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-12
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多