I am currently studying Wordpress AJAX but keep getting error message (so the success function is not being executed)
Currently I have changed the header.php (of twentythirteen theme) to include JavaScript that sends AJAX Request.
<script src="<?php echo get_template_directory_uri(); ?>/js/html5.js"></script>
<![endif]-->
<?php wp_head(); ?>
<script type="text/javascript">
jQuery(document).ready(function($){
//AJAX
$.ajax({
type: "GET",
dataType: "json",
url: "<?php echo admin_url('admin-ajax.php'); ?>;",
data: {
action: 'xn_get_event'
},
success: function(response){
alert("Yes");
},
error: function(response){
alert("omg!");
}
}); //END OF AJAX (SEND REQUEST)
}); //END OF READY FUNCTION
</script>
And then in the theme folder, I have included another PHP file called json-feed.php inside functions folder (so theme_name/functions/json-feed.php) and here I included the following code:
header('Content-Type:application/json');
//Needs to include wp-load.php file!
if(file_exists('../../../../wp-load.php')):
include '../../../../wp-load.php';
else: //Double checking in case the path is wrong.
include '../../../../../wp-load.php';
endif;
//AJAX Handler
add_action('wp_ajax_nopriv_xn_get_event', 'xn_get_event');
add_action('wp_ajax_xn_get_event', 'xn_get_event');
function xn_get_event(){
echo "Wow!";
}
And then when I go to the website it keeps alerting "Omg!" to me and I do not know why it is keep returning error response!