plugin development - Two same AJAX calls - one is working, other doesn't

admin2025-06-06  9

I am developing a WP plugin. I have two same AJAX calls, one is working, and second one is giving me 400 Bad Request error for admin-ajax.php.

ReadyState is 4, responseText is: " ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵0", statusText is "Bad Request", error is POST http://localhost/wc_addtocart_as_admin/wp-admin/admin-ajax.php 400 (Bad Request)

Both calls are getting data from two separated <form> elements. When I change data in working AJAX to use array defined in js code, like in example below, it gives me the same error like the second AJAX call which is not working at all.

    var test = [];
            test[0] = {name: "reason", value: 'OLALA'};
            test[1] = {name: "second", value: 'TRUS'};

    jQuery.ajax({
                url: test_ajax_object.ajax_url,
                type: 'POST',
                data: test

                etc...

                })

So, only first AJAX works with first <form> data. And second one, which is absolutely the same, doesn't.

What could be possible reasons for this weird behavior? Please let me know if I should provide some more info.

I am developing a WP plugin. I have two same AJAX calls, one is working, and second one is giving me 400 Bad Request error for admin-ajax.php.

ReadyState is 4, responseText is: " ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵ ↵0", statusText is "Bad Request", error is POST http://localhost/wc_addtocart_as_admin/wp-admin/admin-ajax.php 400 (Bad Request)

Both calls are getting data from two separated <form> elements. When I change data in working AJAX to use array defined in js code, like in example below, it gives me the same error like the second AJAX call which is not working at all.

    var test = [];
            test[0] = {name: "reason", value: 'OLALA'};
            test[1] = {name: "second", value: 'TRUS'};

    jQuery.ajax({
                url: test_ajax_object.ajax_url,
                type: 'POST',
                data: test

                etc...

                })

So, only first AJAX works with first <form> data. And second one, which is absolutely the same, doesn't.

What could be possible reasons for this weird behavior? Please let me know if I should provide some more info.

Share Improve this question asked Nov 11, 2018 at 11:45 Tahi ReuTahi Reu 3081 silver badge14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

They're not the same though. Look at the form data on each one. The second one, the one that doesn't work, has _test appended to the parameter names and the action value.

The reason you're getting a 400 error is because you're not sending a valid action. The action parameter is how WordPress determines which callback to use to handle the request. If you don't send action with a valid value, no callback exists to handle the request and it returns a 400 error.

You need to make sure the parameter is called action, not action_test, and the value is the proper name you're using in PHP. In your case that appears to be return_reason, not return_reason_test.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749192319a317134.html

最新回复(0)