0

I'm searching for an answer here for hours, and I can't find what's wrong with my code... my wordpress ajax keeps responding "0" no matter what I do... Here's the code:

header.php file:

wp_enqueue_script('js_'.$__page, NEVAL_ASSETS."js/js_course_{$__page}.js", array("jquery"));

js_course_all.js File:

var filter = jQuery("#input_filter").val()+"%";

var data = {
    "action": "course_showall_refresh",
    "data": {
        "sort": _sorting.col,
        "sort_dir": (_sorting.dir) ? 'asc':'desc',
        "filter": filter
    }
};

jQuery.post(ajaxurl, data, function(response){
    alert(response);
});

view .php file:

function refresh(){
    echo 'ajax_response';
    die();
}
add_action("wp_ajax_course_showall_refresh", "refresh");
add_action("wp_ajax_nopriv_course_showall_refresh", "refresh");

The .JS is being correctly imported (the alert goes off with a "0" answer, other functions (UI functions) work correctly.

Akkagi
  • 1
  • 1

2 Answers2

0

Try doing this. It might solve your problem.

jQuery.post(ajaxurl, data, function(response){
    alert(response);
}, 'text');
jrath
  • 980
  • 4
  • 14
  • Tried it, didn't work. I did some debugging by altering the admin-ajax.php file... the 0 being returned is from the default value, and by "die()"ing right before the line "do_action" from that file, echoing the $_POST superglobal, it does return the post superglobal. So the problem is definitely the add_action() not working. Any ideas? – Akkagi Jul 24 '15 at 20:30
  • Though not sure..Is there any need of die()? Try using exit() it may solve the issue. – jrath Jul 24 '15 at 20:39
  • http://stackoverflow.com/questions/1795025/what-are-the-differences-in-die-and-exit-in-php..it may help you. – jrath Jul 24 '15 at 20:43
  • die and exit are the same thing, your link itself says that, too :P... In wordpress, that die/exit is necessary, and no, it didn't work. – Akkagi Jul 24 '15 at 20:45
  • I do not have much idea in wordpress and php. Just an assumption. – jrath Jul 24 '15 at 20:47
0

Are you doing this request on an admin page or frontend? Because ajaxurl is just defined in /wp-admin/. You could try this in your Javascript:

var ajaxurl = "<?= admin_url('admin-ajax.php'); ?>"; jQuery.post(ajaxurl, data, function(response){ [...] }

luklapp
  • 205
  • 1
  • 3
  • 13
  • It is an admin page. I'm coding a plugin for a client. The ajaxurl var is returning the admin-ajax.php as usual, I just can't get the hook to call my function – Akkagi Jul 27 '15 at 14:20
  • Maybe try this Action: wp_ajax_refresh – luklapp Jul 27 '15 at 14:25