$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Two same AJAX calls - one is working, other doesn't|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin10PV0评论

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.

Post a comment

comment list (0)

  1. No comments so far