A
A
Alexander Sobolev2018-06-28 12:23:54
WordPress
Alexander Sobolev, 2018-06-28 12:23:54

Bad request in POST ajax Wordpress. How to fix?

Part of JS code:

jQuery( "#edit_dialog" ).dialog({
      title: "Редактирование услуги",
      resizable: false,
      height: "auto",
      width: 400,
      modal: true,
      buttons: {
        "Сохранить изменения": function() {
          var upd_servise_label = jQuery('#upd_servise_label').val();
          var upd_servise_cost = jQuery('#upd_servise_cost').val();
          var servise_data = {
              'action': "edit_servise",
              'servise_id' : servise_id,
              'current_servise_key' : current_servise_key,
              'current_servise_label' : current_servise_label,
              'current_servise_cost' : current_servise_cost,
              'upd_servise_label' : upd_servise_label,
              'upd_servise_cost' : upd_servise_cost,
            };					
            jQuery.ajax({
              type:"POST",
              url: ajaxurl,							
              dataType: 'json',
              data: "data="+JSON.stringify(servise_data),
              success:function(data){ console.log(data);  },
              error: function(errorThrown){ console.log(errorThrown); } 
            });

          
        },
        "Отмена": function() {  jQuery( this ).dialog( "close" );   }
      }
    });

PHP part:
if($_POST['data']) {
    $data = json_decode($_POST['data']);
     $servise_id = get_text($data->servise_id);
     echo json_encode($servise_id);
     
}

Task : pass json to ajax post processing
Console writes : /wp-admin/admin-ajax.php 400 (Bad Request)
Question : how to pass json to WP?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question