admin管理员组文章数量:1130349
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 badges1 Answer
Reset to default 0They'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.
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 badges1 Answer
Reset to default 0They'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.
本文标签: plugin developmentTwo same AJAX callsone is workingother doesn39t
版权声明:本文标题:plugin development - Two same AJAX calls - one is working, other doesn't 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749192287a2330377.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论