Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

113
Visualizações
WordPress admin-ajax returns bad request 400

In a template file, I need to get the new data from the frontend when a user double-clicks and changes the record on a table. I tested and the console shows that the request is correct, however whatever I tried the admin-Ajax returns always Bad Request! The PHP code waiting for the request in the template file is:

add_action( "wp_ajax_update_records", "update_records" );
add_action( "wp_ajax_nopriv_please_login", "please_login" );
function update_records(){
    global $wpdb;
    $colName = "'" . $_POST['upd_column'] . "'";
    $wpdb->update('Reservations', 
        array(
          $colName => $_POST['upd_value']
        ), 
        array( 'ReservationID' => $_POST['record_id'] ) 
    );
    wp_die();
}; 
function please_login() {
   echo "You must log in to work";
   die();
}

The jQuery code sending the request is:

jQuery('#work td').dblclick(

  function dolly_script() {
    var text = jQuery(this).text();
    var headerName = jQuery(this).attr('headers');
    jQuery(this).text('');
    jQuery('<input />').appendTo(jQuery(this)).val(text).select().blur(

      function() {
        var newVal = jQuery(this).val();
        var selectionID = jQuery(this).closest("tr").children("td[headers='ReservationID']").text(); 
        jQuery(this).parent().text(newVal).find('input').remove();
        jQuery.ajax({
            url: '../wp-admin/admin-ajax.php',
            type: 'POST',
            dataType : "json",
            data: JSON.stringify({
              action: 'update_records',
              record_id: selectionID,
              upd_column: headerName,
              upd_value: newVal
            })
        })
     })
  });

I tried also without JSON.stringify and without datatype.
In functions file I have also added:

wp_register_script( 'dolly_script', '/wp-content/themes/bootcms/work.js');
$myAjax = array( 'ajax_url' => admin_url( 'admin-ajax.php' ) );
wp_localize_script( 'dolly_script', 'ajax_url', $myAjax );
wp_enqueue_script( 'dolly_script' );

Inside the "add_action( 'wp_enqueue_scripts', 'myfunction' );" declaration.

If anyone could help is hugely appreciated, I spent a whole day but I found no solution.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I solved the problem. Here is the solution in case it helps others:

I moved the PHP function into the functions.php file.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda