wordpress plugin admin-ajax 400 bad request

Have tried multiple solutions to fix issue, including using the code examples from the Codex. I have a custom page in the admin that has a form to submit changes to a title. The action fires, but I am getting 400 error. The path is correct, so I am not sure where to go from here. Could use some advice on how to troubleshoot the issue.

Here is my code…

<?php
add_action( ‘admin_footer’, ‘add_ajax_scripts’ );
function add_ajax_scripts() {
/*

ISSUE WITH AJAX — Getting 400 HTTP ERROR — Possible cause is Plugin Conflict or Server Config
Error also occurs with the boilerplate code in the WP Documentation: https://codex.wordpress.org/AJAX_in_Plugins
Included alternative solution as a temporary workaround

*/
?>
<script>
jQuery(‘form’).on(“submit”, function (e) {
e.preventDefault();
var product_id = jQuery(this).find(‘input’).val();
if(jQuery(this).find(‘#name_manual’).val() != “”) {
var product_name = jQuery(this).find(‘#name_manual’).val();
}
else {
var product_name = jQuery(this).find(‘#name_select’).val();
}
jQuery.ajax({
url: ‘/wp-admin/admin-ajax.php’,
type: ‘POST’,
data : {
‘action’: ‘handle_request’,
‘updated_name’: product_name,
‘pid’: product_id
},
success : function( response ){ console.log(response) },
error : function(error){ console.log(error) }
})
});
</script>
<?php
}
add_action( ‘wp_ajax_handle_request’, ‘handle_request’ );
function handle_request() {
global $wpdb;
$pid = $_POST[‘pid’];
$updated_name = $_POST[‘product_name’];
echo $pid;
wp_die(); // this is required to terminate immediately and return a proper response
}

I have spent a while trying a combination of things, but can not find a solution. The server logs are not telling me anything more than what I already know. From what I can tell it should work, but for some reason the 400 error is happening.

Read more here:: wordpress plugin admin-ajax 400 bad request

Leave a Reply

Your email address will not be published. Required fields are marked *