400 Bad Request getting on AJAX Call

Question

I try, when click the submit button then pass the ajax request with some values then calculate that values and return.

My Problem : when I make custom Ajax Call then I get 400 error from admin-ajax.php.
Please help me to find my mistake or best way to do this.

Ajax Call – footer.php(theme footer)

 <script>
    $(function() {

       $("#calc_form").on("submit", function(e) { 
        //   alert("submit");
            e.preventDefault();

            $.ajax({
                method: "POST",
                url: "/wp-admin/admin-ajax.php",
                data: {
                    action: 'calculation',
                    message_id: $('#tot_tax_three_month_Input').val()
                },
                dataType: 'json',
                success: function (output) {

                }

            });
       });

    });
</script>

Admin-ajax.php

// handle the ajax request
 function calculation() {
     $value = $_REQUEST['message_id'];

     echo json_encode(array("value" => $value));
     die();
 }

     // register the ajax action for authenticated users
     add_action('wp_ajax_calculation', 'calculation');

     // register the ajax action for unauthenticated users
     add_action('wp_ajax_nopriv_calculation', 'calculation');

test.php

  <form id="calc_form">
    <table class="table">
      <thead>
        <tr>
          <th style="width: 40%" scope="col"></th>
          <th scope="col">Per month LKR</th>
          <th scope="col">For 3 months period* LKR</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td colspan="2" class=" text-danger"><b>Tax amount for 3 months</b></td>
          <td><input type="text" name="" id="tot_tax_three_month_Input" class="form-control "  /></td>
        </tr>
        <tr>
          <td colspan="2"></td>
          <td>
            <button type="submit" id="btnCalculate" class="btn btn-success">Calculate</button>
            <button type="button" id="btnReset" class="btn btn-secondary">Reset</button>
          </td>
        </tr>
      </tbody>
    </table>
  </form>
0
, Rika 4 years 2020-03-05T07:51:20-05:00 0 Answers 82 views 0

Leave an answer

Browse
Browse