admin管理员组

文章数量:1278825

I try tu run wp form with ajax but console return Wordpress admin-ajax.php 400 bad request.

 jQuery(function ($) {
        $(document).ready(function () {
    
    
            jQuery('#form_add_to_cart').on('submit', function () {
                
                jQuery.ajax({
                    url: '.php', 
                    method: 'post',
                    contentType : 'application/json; charset=utf-8',
                    data: $("#form_add_to_cart").serializeArray(),
                    success: function (response) {
                        alert(response);
                    },
                    fail: function (err) {
                        alert("There was an error: " + err);
                    }
                });
                return false;
            });
        });
    });

here is my functions.php file:

add_action('wp_ajax_send_form', 'send_form'); // This is for authenticated users
add_action('wp_ajax_nopriv_send_form', 'send_form'); // This is for unauthenticated users.

function send_form(){
    if (isset($_POST['send_form'])){
        echo'ok';
    }
    else{
        echo 'bad';
    }
}

and my form:

            <form name="form_add_to_cart" method="POST" class="form_product" id="form_add_to_cart" action="">

                        <input hidden="hidden" name="action" id="add_product_to_cart"
                               value="send_form">
                        <input class="btn btn-primary" type="submit"
                               name="add_to_cart-<?php echo $product_id; ?>" id="add_to_cart"
                               value="Dodaj">

all form :

WORKING:

jQuery(function ($) {
        $(document).ready(function () {


            jQuery('#form_add_to_cart').on('submit', function () {

                jQuery.ajax({
                    url: '.php', 
                    method: 'post',
                    data: $("#form_add_to_cart").serializeArray(),
                    success: function (response) {
                        alert(response);
                    },
                    fail: function (err) {
                        alert("There was an error: " + err);
                    }
                });
                return false;
            });
        });
    });

I try tu run wp form with ajax but console return Wordpress admin-ajax.php 400 bad request.

 jQuery(function ($) {
        $(document).ready(function () {
    
    
            jQuery('#form_add_to_cart').on('submit', function () {
                
                jQuery.ajax({
                    url: 'https://domain/wp-admin/admin-ajax.php', 
                    method: 'post',
                    contentType : 'application/json; charset=utf-8',
                    data: $("#form_add_to_cart").serializeArray(),
                    success: function (response) {
                        alert(response);
                    },
                    fail: function (err) {
                        alert("There was an error: " + err);
                    }
                });
                return false;
            });
        });
    });

here is my functions.php file:

add_action('wp_ajax_send_form', 'send_form'); // This is for authenticated users
add_action('wp_ajax_nopriv_send_form', 'send_form'); // This is for unauthenticated users.

function send_form(){
    if (isset($_POST['send_form'])){
        echo'ok';
    }
    else{
        echo 'bad';
    }
}

and my form:

            <form name="form_add_to_cart" method="POST" class="form_product" id="form_add_to_cart" action="">

                        <input hidden="hidden" name="action" id="add_product_to_cart"
                               value="send_form">
                        <input class="btn btn-primary" type="submit"
                               name="add_to_cart-<?php echo $product_id; ?>" id="add_to_cart"
                               value="Dodaj">

all form : https://pastebin/YJXTjGjJ

WORKING:

jQuery(function ($) {
        $(document).ready(function () {


            jQuery('#form_add_to_cart').on('submit', function () {

                jQuery.ajax({
                    url: 'https://domain/wp-admin/admin-ajax.php', 
                    method: 'post',
                    data: $("#form_add_to_cart").serializeArray(),
                    success: function (response) {
                        alert(response);
                    },
                    fail: function (err) {
                        alert("There was an error: " + err);
                    }
                });
                return false;
            });
        });
    });
Share Improve this question edited Sep 23, 2021 at 13:12 NCTI asked Sep 23, 2021 at 11:00 NCTINCTI 131 gold badge1 silver badge5 bronze badges 2
  • "i have multiple forms for each product in my page, but ajax working only for first form" -you've used '#form_add_to_cart' as a selector. That potentially only selects a single element with id=form_add_to_cart: ids ought to be unique on the page. – Rup Commented Sep 23, 2021 at 11:42
  • Ok I change selector from ID to class, now click working, but still error 400 – NCTI Commented Sep 23, 2021 at 11:44
Add a comment  | 

2 Answers 2

Reset to default 2

Your full form code does contain a hidden input named action with the value send_form.

However, you're not sending it correctly, because of the the contentType part in your JS script, which submits the form data as a JSON payload, but note that the old admin-ajax.php endpoint doesn't support JSON payload, hence $_REQUEST['action'] would be empty unless if you added it to the URL query string, and therefore WordPress won't know what the AJAX action is. So just remove the following from your JS code and the error would be gone:

contentType : 'application/json; charset=utf-8', // remove this

Additionally, your form doesn't have an input named send_form, so in your send_form() function, this will fail: if (isset($_POST['send_form'])). So you should add the input to your form or change the if condition.

Also, you should call wp_die() at the end in your function to exit the page and prevent WordPress from printing a 0 (zero).

In the data property you need to pass the action to be called with the rest of your data.
Based on you ajax action you action should be send_form

data: {
    action: 'send_form',
    formData: $("#form_add_to_cart").serialize()
}

Now in your php callback function you can do print_r($_POST) to see what the post request contains.

本文标签: Wordpress adminajaxphp 400 bad request