I'm trying to make an ajax call on a form submit in WP. Call seems to be good, but response is always 0 ...
I read that it meant that my call function was not found. But I believe my call is right.
I declare my ajax object :
function sparcraft_scripts() {
$ajax = array('ajax_url' => admin_url('admin-ajax.php'));
wp_register_script('sparcraft-js-configurator-greement', get_stylesheet_directory_uri().'/js/sparcraft-configurator-greement.js');
wp_localize_script('sparcraft-js-configurator-greement', 'ajax_object', $ajax);
wp_enqueue_script('sparcraft-js-configurator-greement');
}
add_action('wp_enqueue_scripts', 'sparcraft_scripts');
This is my JS script :
jQuery(function($) {
$(document).ready(function () {
$('#sparcraft-configurator-form').submit(function(e) {
e.preventDefault();
$.ajax({
url: ajax_object.ajax_url,
method: 'POST',
data: {
action: 'sparcraft_configurator_form',
id: 1,
},
success: function(data) {
console.log(data);
},
error: function(data) {
console.log('Error : ' + JSON.stringify(data));
}
});
});
});
});
And my PHP script :
add_action('wp_ajax_sparcraft_configurator_form', 'sparcraft_configurator_form');
add_action('wp_ajax_nopriv_sparcraft_configurator_form', 'sparcraft_configurator_form');
function sparcraft_configurator_form() {
$data = array("response" => "yes");
echo json_encode($data, true);
wp_die();
}
I don't understand why 0 is the only response for that call. What did I do wrong ?
Thanks for the help !