B
B
Basil2832020-04-14 01:23:16
WordPress
Basil283, 2020-04-14 01:23:16

Why is wordpress+ajax data not being passed?

I'm trying to figure out with AJAX + wordpress. Created a form, a script and a test function. But in the alert message, all data is displayed, but the function does not display anything.

if(!function_exists('my_custom_script')):
    function my_custom_script(){

        wp_enqueue_script( 'jquery' );
        wp_enqueue_script('customajax', get_stylesheet_directory_uri() . '/js/custom.js', array(), 1.0,false
      );

       wp_localize_script('customajax', 'ajjax', array(
                'url'   => admin_url( 'admin-ajax.php' ),));
}
endif;
add_action('wp_enqueue_scripts','my_custom_script');


if(!function_exists('ret')):
function ret(){
  
    $title = $_POST['titleObject'];
    echo $title;
}
endif;
add_action( 'My', 'ret' );
if( defined('DOING_AJAX') ){
add_action('wp_ajax_qwas', 'ret');
add_action('wp_ajax_nopriv_qwas', 'ret');
}


jQuery(function(jQuery) {
    jQuery('#custom_button').click(function () {
        jQuery.ajax({
                type: "POST",
            dataType: 'json',
            data: {
                    action: 'qwas',
                    titleObject: 5,
                },
                url: ajjax.url,
                success: function (response) {
                    alert(response);
                }
        });

    });
});

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
KingAnton, 2020-04-14
@KingAnton

Look for the error in the browser console

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question