I have tried going through all of the posts to do with the URL not being defined however none have fixed it.
My code in index:
wp_enqueue_script('BbingoAppG', plugin_dir_url(__FILE__) . 'js/formProcess.js', array ( 'jquery' ), 0.1, false);
wp_localize_script( 'BbingoAppG', 'bbingo2_obj', array('ajax_url' => admin_url( 'admin-ajax.php' ) ));
Then lower down:
add_action( 'wp_ajax_bsetter_ajax_request', 'bsetter_ajax_request' );
add_action( 'wp_ajax_nopriv_bsetter_ajax_request', 'bsetter_ajax_request' );
The function being called:
function submitCard(newPartner){
alert(newPartner);
jQuery.ajax({
url: ajax_url, // Since WP 2.8 ajaxurl is always defined and points to admin-ajax.php
data: {
'action':'bsetter_ajax_request', // This is our PHP function below
'gamecard': JSON.stringify(newPartner)
},
success:function(data) {
// This outputs the result of the ajax request (The Callback)
alert("Successfully added a partner");
jQuery("#newPForm").text(data);
},
error: function(errorThrown){
window.alert(errorThrown);
}
});
}
Any help would be greatly appreciated :)
if ajax url is not defined it might be because your JS script is called before it is declared. Make sure to add your JS in your functions.php with the wp_register_script hook.
If you want a quick fix or test the ajax endpoint, the base url is
/admin-ajax.php?action=bsetter_ajax_request.
function.php
// exemple
function my_enqueue_scripts()
{
wp_register_script( 'myscript', get_template_directory_uri() . 'js/myscript.js' );
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );