I’m having trouble with my WordPress AJAX implementation. When I try to send a request, I keep getting a 400 Bad Request error.
Error message I’m seeing:
POST http://localhost/.../wp-admin/admin-ajax.php 400 (Bad Request)
My JavaScript code:
$.ajax({
type: "POST",
url: ajax_vars.ajax_url,
data: {
action: 'create_new_password'
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
var result = JSON.parse(response);
$('#password_field').val(response);
alert(JSON.parse(response));
},
error: function(xhr) {
console.log(xhr);
}
}).fail(function() {
console.log("request failed");
});
PHP handler code:
add_action('admin_enqueue_scripts', 'load_custom_scripts');
function load_custom_scripts() {
wp_register_script('password-handler', $plugin_url . 'js/password-handler.js', array('jquery'), '1.0.0', true);
wp_enqueue_script(array('password-handler'));
$variables = array('ajax_url' => admin_url('admin-ajax.php'));
wp_localize_script('password-handler', 'ajax_vars', $variables);
}
add_action('wp_ajax_create_new_password', 'handle_password_creation');
function handle_password_creation() {
$new_password = (string)wp_generate_password(8, true, false);
echo $new_password;
header('Content-Type: application/json');
$output = json_encode($new_password);
echo $output;
exit;
}
I’ve looked at other similar posts but none of the suggested fixes worked for me. I’m pretty new to WordPress development so I might be missing something obvious. Any help would be appreciated!