Wordpress插件Ajax不执行功能

时间:2018-06-16 09:14:02

标签: php ajax wordpress plugins

我正在尝试在wordpress中执行ajax调用,但不在函数php中执行代码。

Jquery

jQuery(document).ready( function($){
    $("#formato").change(function(){
        var formato_rassegna = $("#formato").val();
        jQuery.ajax({
            url : my_vars.ajaxurl,
            type : 'post',
            data : {
                action : 'filtroformato',
                tipologia : formato_rassegna
            },
            success : function( response ) {
                console.log(formato_rassegna);
            }
        });

    });
});

Plugin.php

    function adminprintdoc_stylescriptelement_init() {
        wp_enqueue_style('adprdocasweome-style','https://use.fontawesome.com/releases/v5.0.9/css/all.css');
        wp_enqueue_style('adprdochover-style', WP_PLUGIN_URL . '/rassegnastampa/css/animation.css');
        wp_enqueue_style('adprdoc-style', WP_PLUGIN_URL . '/rassegnastampa/css/rassegnastampa.css');

        wp_enqueue_script( 'my-custom', WP_PLUGIN_URL . '/rassegnastampa/js/rassegnastampa.js', array( 'jquery' ) );
        wp_localize_script( 'my-custom', 'my_vars', array(
            'ajaxurl'   => admin_url( 'admin-ajax.php' )));
        }
    add_action('init', 'adminprintdoc_stylescriptelement_init');
}

function filtroformato() {
    echo "ciaoooooo";
    die();// chiudo l'esecuzione della chiamata AJAX, evitando di far ritornare errori da parte di WordPress
}
add_action( 'wp_ajax_filtroformato', 'filtroformato' );

Classe.php

 <form>
                <div><p>Formato:</p><select name="formato" id="formato">
                        <option>indifferente</option><option>cartaceo</option><option>web</option><option>social</option>
                    </select></div>
                <div>
</form>

函数javascript工作正常但不执行php函数中的代码filtroformato()...为什么?

0 个答案:

没有答案