Нет ответа от AJAX в WordPress - PullRequest
0 голосов
/ 04 марта 2020

В моем шаблоне у меня есть кнопка, и при нажатии я должен получить «Далее нажата». Затем, когда почтовый запрос получает ответ, я должен получить ответ «Получил ответ!» в консоли, но я получаю только первое сообщение.

Это в моем js / inputtitle_submit. js:

jQuery(document).ready(function($) {
        console.log('js loaded!');
        $('#next').click(function () {
            console.log('Next is clicked!');
            var data = {
                'action': 'myajax_inputtitleSubmit_func', // wp ajax action
                'title': $('input[name=title]').val(), // vars
                'nextNonce': PT_Ajax.nextNonce // send the nonce along with the request
            };
            $.post(PT_Ajax.ajaxurl, data, function(response){
                console.log('Got response!');
            });
        });
    });

Это мое funtion. php:

add_action( 'wp_enqueue_scripts', 'twentyseventeen_child_scripts' );
add_action( 'wp_ajax_myajax_inputtitleSubmit_func', 'myajax_inputtitleSubmit_func' );
add_action( 'wp_ajax_nopriv_myajax_inputtitleSubmit_func', 'myajax_inputtitleSubmit_func' );

//Function to load js an localize vars
function twentyseventeen_child_scripts() {
    wp_enqueue_script( 'inputtitle_submit', '/wp-content/themes/twentyseventeen-child' . '/js/inputtitle_submit.js', array( 'jquery' ) );
    wp_localize_script( 'inputtitle_submit', 'PT_Ajax', array(
        'ajaxurl'   => admin_url( 'admin-ajax.php' ),
        'nextNonce' => wp_create_nonce( 'myajax-next-nonce' )
        )
    );
}

//Function to return a response to an ajax call
function myajax_inputtitleSubmit_func() {
    // check nonce
    $nonce = $_POST['nextNonce'];
    if ( ! wp_verify_nonce( $nonce, 'myajax-next-nonce' ) ) {
        die ( 'Busted!' );
    }
    echo "response";
    exit;
}

1 Ответ

0 голосов
/ 04 марта 2020

Я написал код с некоторыми изменениями. Надеюсь, он работает для вас

add_action( 'wp_enqueue_scripts', 'twentyseventeen_child_scripts' );
//Function to load js an localize vars
function twentyseventeen_child_scripts() {

   wp_enqueue_script( 'inputtitle_submit', get_stylesheet_directory_uri().'/js/inputtitle_submit.js', array( 'jquery' ), '1.0', true );

    $data = array(
        'ajaxurl'=> admin_url( 'admin-ajax.php')
    );
    wp_localize_script( 'inputtitle_submit', 'PT_Ajax', $data );


}

add_action( 'wp_ajax_myajax_inputtitleSubmit_func', 'myajax_inputtitleSubmit_func');
add_action( 'wp_ajax_nopriv_myajax_inputtitleSubmit_func', 'myajax_inputtitleSubmit_func');
function myajax_inputtitleSubmit_func(){

    $myArr = array(
        'response' => 'xyz'
    );
    $myJSON = json_encode($myArr); 
    echo $myJSON;
    die();
}

Past on External js файл

jQuery(document).ready(function($) {

        jQuery(document).on('click', '#next', function(){

            var title = jQuery('input[name=title]').val();

            jQuery.ajax({
                url: PT_Ajax.ajaxurl,
                type: "POST",
                data: {'action': 'myajax_inputtitleSubmit_func', title: title},
                cache: false,
                dataType: 'json',
                beforeSend: function(){
                },
                complete: function(){
                },
                success: function (response) { 

                    console.log(response);
                }
            });

        });

    });

Я протестировал этот код работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...