最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Admin Ajax always return 0

programmeradmin0浏览0评论

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 !

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论