简体   繁体   中英

Ajax with wordpress, 400 bad request

I try to submit a form using Ajax, working in a plugin. I developed two plugins the first one was working but not anymore and I didn't find any mistakes. I think that is not coming from the code it self but I'm a bit lost.

Here is my 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';

and my 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 ) {
...
}

Try below code and if it couldn't work then check following things - Change URL in below code -

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

Checklist

  1. AJAX URL ( for wordpress default is your_site_url/wp-admin/admin-ajax.php)
  2. DataType could be 'json', 'html', 'xml' Check what server is delivering you though your request

Hope this could help. Happy Coding.

I was posting files and there were to big,

So in /etc/php/7.0/apache2/php.ini increase

upload_max_filesize = 13M
post_max_size = 27M

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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