Search code examples
phpajaxwordpressadmin-ajax

POST admin-ajax throwing error 400 - not finding custom functions


I am adding a function that should be called when a button is clicked, i have a js file with the following jquery code and the ajax call :

jQuery(document).ready( function() {

   function getUrlParameter(sParam){

      var sPageURL = window.location.search.substring(1);

      var sURLVariables = sPageURL.split('&');

      for (var i = 0; i < sURLVariables.length; i++) {
         var sParameterName = sURLVariables[i].split('=');
         if (sParameterName[0] == sParam) {
            return sParameterName[1];
         }
      }
   }

   jQuery('#upload_btn').click( function(e) {

      e.preventDefault(); 

      var flag = true;
      var postId = getUrlParameter('preview_id');
      var files = jQuery('#file_tool').prop('files');
      console.log(files);

      var dataS = {
         'action': 'upload_button',
         'preview_id': postId,
         'files': files,
         'set': flag
      }
      jQuery.ajax({
         type : 'post',
         url : diario.upload,
         processData: false,
         contentType: false,
         data : dataS,
         success: function(response) {
            if(response.type == "success") {
               console.log('jquery works');
            } else console.log(response);
         }
      });

   });

 });

When i click the button the console.log shows the files obj so at least the onClick works, but just after that it shows up jquery.js:4 POST custom/wp-admin/admin-ajax.php 400. This is how my functions.php looks like :

add_action( 'wp_enqueue_scripts', 'enqueue_onClick_upload' );

//custom_js_enqueuer
function enqueue_onClick_upload() {

    wp_register_script( 'onClick_upload', WP_CONTENT_URL.'/themes/microjobengine/onClick_upload.js', array('jquery') );
    wp_localize_script( 'onClick_upload', 'diario', array( 'upload' => admin_url( 'admin-ajax.php' )));        

    wp_enqueue_script( 'jquery' );
    wp_enqueue_script( 'onClick_upload' );

}


add_action('wp_ajax_upload_button', 'upload_button');
add_action('wp_ajax_nopriv_upload_button', 'upload_button');

// upload button function
function upload_button() {

    $postID = $_POST['preview_id'];

    if ($_POST['set']) {
        if($_POST['files']['size'] === 0 ) 
            echo "<script>console.log('There's no images.');</script>";

    }
    $result['type'] = 'success';
    echo json_encode($result);
    wp_die();
}

I have no idea why it doesn't find the action, it's all done as wp says it should, I hope somone could give a hand, thanks.

--EDIT--

Okay so now i tried to only pass 'action': 'upload_button', the error does not appear but the response doesn't get success, I did this with all the code inside my function commented and just leaving the las 3 lines, in order to return the success, but it doesn't, so it might find the function but for some reason it doesn't get performed, and of course that means something wrong happens when i pass the extra data, any thoughts about why this happens?


Solution

  • Sorry i wasn't getting the right thing to get feedback from the functions, i just had to delete all echos and save everything i needed into an array and returne it json encoded.