Wordperss admin-ajax.php error 400 Bad Request

224 views Asked by At

I can't find where this error 400 comes from?

FontOffice only avaible for logged in persons.

I'm trying to create a form to post a custom post type through Ajax.

Here is the form in front office:

<form id="form_info" enctype="multipart/form-data">
     <div class="form-group">
            <label for="titre_info">Titre *</label>
            <input type="text" id="titre_info" name="titre_info" placeholder="Titre" />
     </div>
     <div class="form-group">
            <label for="message_info">Message *</label>
            <textarea id="message_info" name="message_info" placeholder="Message"></textarea>
     </div>
     <div class="form-group">
            <label for="img_info">Visuel (facultatif)</label>
            <input type="file" id="img_info" name="img_info" />
     </div>
     <div style="text-align:center;">
            <input type="submit" id="submit_info" name="submit_info" value="Poster" />
            <div class="status_info"></div>
      </div>
  </form>

Here is my JQuery :

$('#form_info').on('submit',function(e){
        $('.status_info').hide();
        e.preventDefault();
        var validForm = true;
        var title = $("#titre_info").val();
        var message = $('#message_info').val();
        var image = $('#img_info').prop('files');
        if(title=="" || message==""){
            validForm=false;
        }
        if(validForm) {
            $('.status_info').show().html("<p>Envoi du message en cours...</p>");
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType: false,
                processData: false,
                url: '/wordpress/wp-admin/admin-ajax.php',
                data: {
                    'action': 'addinfo',
                    'titre': title,
                    'message': message,
                    'image': image
                },
                success: function (data) {
                    $('.status_info').show().html("<p class='success'>Merci de votre ajout</p>");
                    setTimeout(function(){location.reload();},750);
                }
            })
        } else {
            $('.status_info').show().html("<p class='error'>Merci de compléter les champs obligatoires</p>");
            $("#titre_info").addClass('error');
            $("#message_info").addClass('error');
        }
    });

Here is my function PHP :

add_action( 'wp_ajax_nopriv_addinfo', 'AddInfo' );
add_action( 'wp_ajax_addinfo', 'AddInfo' );
function AddInfo(){
     $titre=$_POST['titre'];
     $message=$_POST['message'];
     $image=$_POST['image'];

     $my_post = array(
         'post_title' => $titre,
         'post_content' => $message,
         'post_status' => 'publish',
         'post_type' => 'annonce',
     );
     $my_new_post = wp_insert_post($my_post);

     echo json_encode(array('post'=>$my_new_post,"status"=>"succes"));
     die();
}

It used to work but I then try to add the file upload and then everything skratch. Now returned to basic creating simple text post_type I'm stuck with my Error 400 Bas Request on url admin-ajax.php

I also tried to add the security none, same problem.

I tried to use ajax_object to create the ajax url but same probleme. The Php function change this way :

add_action('init','ajax_register_action');
function ajax_register_action() {
     wp_register_script('ajax-addinfo-script', get_stylesheet_directory_uri() . '/ajax-addinfo-script.js', array('jquery'));
     wp_localize_script('ajax-addinfo-script', 'ajax_addinfo_object', array('ajaxurl' => admin_url('admin-ajax.php'),));
     wp_enqueue_script('ajax-addinfo-script');
     add_action( 'wp_ajax_nopriv_addinfo', 'AddInfo' );
     add_action( 'wp_ajax_addinfo', 'AddInfo' );
 }

And the jQuery call the ajax url this way :

url: ajax_addinfo_object.ajaxurl,

I checked a lot of StackOverflow and Google questions about this error but still can't find where it comes from.

If you can see what's wrong that will be a gret relief!

Thanks a lot!

0

There are 0 answers