A
A
Andrey Yurchuk2019-09-28 21:32:10
WordPress
Andrey Yurchuk, 2019-09-28 21:32:10

400 BAD REQUEST. (Ajax Wordpress) What am I doing wrong?

Karoch wrote a landing page, the task is to implement a feedback form in PHP using JQuery AJAX. And moreover, that the site comes as a theme for WordPress. I did everything as in the dock, used admin-ajax.php, Result: Returns an error (Bad Request 400). Code below. Maybe someone knows where I stuck about.

HTML:

<div class="form-block">
            <form action="" id="header-form"  method="post" class="form">
              <label class="title">ЗАМОВТЕ БЕЗКОШТОВНУ КОНСУЛЬТАЦІЮ</label>
              <label ><span>Ім'я *</span><input autocomplete="off" type="text" name="send_name"></label>
              <label><span>Телефон *</span><input autocomplete="off" placeholder="+38(XXX) XX-XX-XXX" type="text" name="send_phone"></label>
              <label><span>Додаткова інформація</span><input autocomplete="off"  type="text" name="send_info"></label>
              <label><input type="submit" name="subm" value="ЗАМОВИТИ ДЗВІНОК"></label>


JS:

$ ('#header-form').on('submit', function (e) {
        e.preventDefault();
        
        var inf = {
          name: $ ('input[name="send_name"]').val(),
          phone: $ ('input[name="send_phone"]').val(),
          info: $ ('input[name="send_phone"]').val()
        };
        
        $.ajax ({
          url: '<?php echo admin_url('admin-ajax.php') ?>',
          type: 'POST',
          data: inf,
          contentType: 'application/json',
          dataType: 'json',
          beforeSend: function () {
            alert('<?php echo admin_url('form-ajax.php'); ?>');
        },
          success: function (err) {
           $ ('input[name="send_name"]').css('display', 'none');
        },
          error: function (err, rex, ww) {
            alert ('Otladka');
          console.log (err);
          console.log (rex);
          console.log (ww);
        }
        });
      });


PHP (functions.php themes)

add_action ('wp_ajax_sendf', 'sendf');
add_action ('wp_ajax_nopriv_sendf', 'sendf');

function sendf () {
  
  $result = array ();
  
  echo json_encode ($result);
  exit;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
asnever, 2020-06-16
@AnDrIYQ

You don't have an action in the data field. Need like this:

var inf = {
          action: 'sendf',
          name: $ ('input[name="send_name"]').val(),
          phone: $ ('input[name="send_phone"]').val(),
          info: $ ('input[name="send_phone"]').val()
        };

K
Konstantin Malyarov, 2019-09-28
@Konstantin18ko

If Chrome, then press F12, then to the Network tab and find your request.
There is a suspicion that this is csrf.
It is missing from your request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question