0

I'm trying to implement the ajax in wordpress world but now stuck. Function hook suppose to be called when ajax triggered is not happening.

I check the network

  • admin-ajax.php is called but response is null implied that getsomething function is not called.

Anyone have any idea?

Follow are my code.

functions.php

function my_resource() {
    wp_enqueue_script('my-jquery',get_template_directory_uri().'/jqfunctions.js');
    wp_localize_script( 'my-jquery', 'myback', 
    array('ajax_url' => admin_url( 'admin-ajax.php' )));
}
add_action('wp_enqueue_scripts', 'my_resource');

jqfunctions.js

jQuery(document).ready(function(){
jQuery("#popular").click(function(){
    console.log('popular clicked');
        jQuery.ajax({
        url: myback.ajax_url,
        type: 'POST',
        dataType: 'json',
        data:{
            action: 'getsomething'
        },
        success: function( response ){
            console.log("This is response...");
            console.log(response);
        },
        error: function( error ){
            console.log('AJAX error callback....');
            console.log(error);
        }
       });
    });
});

admin-ajax.php

 function getsomething(){
 wp_send_json_error('hey');
 }


 add_action('wp_ajax_nopriv_getsomething', 'getsomething');
 add_action('wp_ajax_getsomething', 'getsomething');
  • "Admin-ajax.php" is the Name of the wordpress file that handles Ajax requests, not a file you have to create. Put Your "admin-ajax.php" code into the functions.php and it should be working! – kuchenundkakao Nov 8 '15 at 8:49
  • Follow right sequence for localizing. 1. Register 2. Localize 3. Enqueue – Sisir Nov 8 '15 at 9:05
  • It's working like u suggested @kuchenundkakao ,shifting my code from admin-ajax.php to functions.php – Pond Pisutaarnon Nov 8 '15 at 10:53
1

I've tested your code and it works perfectly. I've just declared the dependencies for the JavaScript, so it is loaded correctly after jQuery. This is how I've tested it:

functions.php

function my_resource() {
    wp_enqueue_script('my-jquery',get_template_directory_uri().'/jqfunctions.js', array('jquery'));
    wp_localize_script( 'my-jquery', 'myback', 
    array('ajax_url' => admin_url( 'admin-ajax.php' )));
}
add_action('wp_enqueue_scripts', 'my_resource');
function getsomething(){
  wp_send_json_error('hey');
}
add_action('wp_ajax_nopriv_getsomething', 'getsomething');
add_action('wp_ajax_getsomething', 'getsomething');

jqfunctions.js

jQuery(document).ready(function(){
jQuery("#popular").click(function(e){
    e.preventDefault();
    console.log('popular clicked');
    jQuery.ajax({
        url: myback.ajax_url,
        type: 'POST',
        dataType: 'json',
        data:{
            action: 'getsomething'
        },
        success: function( response ){
            console.log("This is response...");
            console.log(response);
        },
        error: function( error ){
            console.log('AJAX error callback....');
            console.log(error);
        }
       });
    });
});
  • 1
    Thank you for testing my code. now it 's working by moving that chuck of code from admin-ajax.php to functions.php – Pond Pisutaarnon Nov 8 '15 at 10:54
  • 1
    Please accept the answer if it solved the problem, to close the question. We should avoid modifying core files, like admin-ajax.php @PondPisutaarnon – birgire Nov 8 '15 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.