Почему не выполняется функция через ajax в wordpress?

Есть вот такой скрипт.
jQuery(document).ready(function ($) {
	// Perform AJAX login/register on form submit
	jQuery('#myavatar').on('submit', function (e) {
        if (!jQuery(this).valid()) return false;
        jQuery('p.status', this).show().html(ajax_profile_object.loadingmessage);
		action = 'ajaxavatar';
		
		ctrl = jQuery(this);
		$.ajax({
            type: 'POST',
            dataType: 'json',
            url: ajax_profile_object.ajaxurl,
            data: {
                'action': action,
				
            },
            success: function (data) {
				jQuery('p.status', ctrl).text(data.message);
				if (data.loggedin == true) {
					var delay = 3000;
                    setTimeout("document.location.href = ajax_profile_object.redirecturl", delay);
                }
            }
        },3000);
        e.preventDefault();
    });
	
});


И функция

function ajax_profile_init(){	

    wp_register_script('ajax-profile-script', get_template_directory_uri() . '/js/js/ajax-profile-script.js', array('jquery') ); 
    wp_enqueue_script('ajax-profile-script');

    wp_localize_script( 'ajax-profile-script', 'ajax_profile_object', array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'redirecturl' => home_url(),
        'loadingmessage' => __('<img id="imgcode" src="http://########.com/loading.gif" alt="Sending user info, please wait...">')
    ));

    // Enable the user with no privileges to run ajax_login() in AJAX
    add_action( 'wp_ajax_nopriv_ajaxavatar', 'my_avatar' );
 
}


function my_avatar(){
 
echo json_encode(array('loggedin'=>false, 'message'=>__('Invalid username or e-mail.')));

	 	die();
}


loadingmessage подгружается, а вот my_avatar не срабатывает....
  • Вопрос задан
  • 2551 просмотр
Решения вопроса 1
lifestar
@lifestar
Wallet карты, Видео-продакшн
1.Чему равна ajax_profile_object.ajaxurl?
2. Что в ответе запроса?
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
@mr_ko
Javascript, Node.js. React.js, Vue.js, Wordpress
Вы не правильно указали action. Ваш action ="ajaxavatar".
add_action( "wp_ajax_Ваш_action", "Ваш_функция_обработки" );
add_action( "wp_ajax_nopriv_Ваш_action", "Ваш_функция_обработки" );
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы