First of all, your js is wrong, you are mixing the syntax of jQuery.post()
and of jQuery.ajax()
You should use
jQuery.ajax({
type: "POST",
url: ajaxurl,
data: { action: "lu_ajax", status: "enabled" },
success: function( data ) {
alert(data);
}
});
or
jQuery.post(
ajaxurl,
{ action: "lu_ajax", status: "enabled" },
function(data) {
alert(data);
}
);
After that the ajaxurl
js variable is available only in admin pages, not in frontend.
So the code above will work only in admin pages, but once you use add_action('wp_ajax_nopriv_lu_ajax', 'lu_ajax');
I think you want to use ajax in frontend, in this case you have to pass the ajax url to the script via wp_localize_script
.
So if the js script in which reside the js code above is names ‘my_ajax.js’ and resides in the ‘js’ subfolder of your plugin, do something like this
add_action('wp_enqueue_scripts', function() {
wp_enqueue_script('my-ajax', plugins_url('js/my_ajax.js', __FILE__) );
wp_localize_script(
'my-ajax',
'myAjaxData',
array( 'ajaxurl' => admin_url('admin-ajax.php') )
);
});
Now the in the js code above replace ajaxurl
with myAjaxData.ajaxurl