text: Q:
  
  AJAX call not work on wordpress - return HTML page everytime
  
  I currently have a recurring problem with my ajax calls.
  I always have the html page that is returned in response.
  I'm sending you a typical example that does not work.
  It is basically a quiz plugin that I developed and sends AJAX calls at each stage. This call populates a database table.
  
  Ajax call on plugin front page
  
      var data = {
          action: 'insert_stat_question',
          security : ajax_var.nonce,
          id_formation: $(document).find('#id_formation').val(),
          id_module: $(document).find('#id_module').val(),
          id_question: button.attr('data-id-question'),
          num_question: button.attr('data-num-question'),
          id_user: $(document).find('#id_user').val(),
          statut: statut
  
      };
  
      jQuery.ajax({
          type:'POST',
          url: ajax_var.url,
          data:data,
          success: function(data){
               // Do your stuff here once ajax response is returned
               //console.log(result);
               console.log( data );
          },
          error: function(errorThrown) {
              console.log(errorThrown);
          },
      });
  
  Plugin file to list all ajax function and register ajax call
  
      function theme_name_scripts() {
      wp_enqueue_script( 'script-name',  plugin_dir_url( __FILE__ ) . 'js/update_stat.js', array('jquery'), '1.0.0', true );
      wp_localize_script( 'script-name', 'ajax_var', array(
      // URL to wp-admin/admin-ajax.php to process the request
      'url' => admin_url( 'admin-ajax.php' ),
      // generate a nonce with a unique ID "myajax-post-comment-nonce"
      // so that you can check it later when an AJAX request is sent
      'nonce' => wp_create_nonce( 'secure_callback' )
      ));
      }
      add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
  
  PHP function call
  
      function insert_stat_question(){
      check_ajax_referer( 'secure_callback', 'security' );
      global $wpdb;
      $wpdb->insert( 
      $wpdb->prefix . 'quizzcreator_stats_question', 
      array( 
      'id_formation' => $_POST['id_formation'], 
      'id_module' => $_POST['id_module'],
      'id_question' => $_POST['id_question'],
      'num_question' => $_POST['num_question'],
      'meta_statut' => $_POST['statut'],
      'id_user' => $_POST['id_user'],
      'date_entry' => date('Y-m-d H:i:s')
      ), 
      array( 
      '%d', 
      '%d',
      '%d', 
      '%d',
      '%s', 
      '%d',
      '%s' 
      ) 
      );
  
      $response = [
          'working' => true
      ];
  
      wp_send_json_success($response);
      }
      add_action( 'wp_ajax_insert_stat_question', 'insert_stat_question' );
      add_action( 'wp_ajax_nopriv_insert_stat_question', 'insert_stat_question' );
  
  I try everything but nothing work.
  Screenshot of headers and response ajax call:
  
  Thank for your help!
  
  A:
  
  Check out wp_send_json_success
  the parameter should be the array of data you want to send back as response.
  e.g.
  $response = [
      'working' => true,
      'data1'     => 'My data 1',
      'data2'     => 'My data 2'
  ];
  
  wp_send_json_success( $response );
  
  in your case, you can do something like,
  $action = $wpdb->insert( 
      $wpdb->prefix . 'quizzcreator_stats_question', 
      array( 
          'id_formation' => $_POST['id_formation'], 
          'id_module' => $_POST['id_module'],
          'id_question' => $_POST['id_question'],
          'num_question' => $_POST['num_question'],
          'meta_statut' => $_POST['statut'],
          'id_user' => $_POST['id_user'],
          'date_entry' => date('Y-m-d H:i:s')
      ), 
      array( 
          '%d', 
          '%d',
          '%d', 
          '%d',
          '%s', 
          '%d',
          '%s' 
      ) 
  );
  
  wp_send_json_success( [ 'action' => $action ] );
  
  Also change your ajax request to something like this,
  $.ajax({
      type: 'POST',
      url: ajax_var.url,
      data: data,
      success: function(data) { 
          console.log( data );
      },
      error: function(errorThrown) {
          console.log(errorThrown);
      },
  });
  
  