I create a function to receive the ajax calls in function.php (wordpress)
add_action( 'wp_ajax_contact_us_receiver', 'contact_us_receiver_callback' );
function contact_us_receiver_callback() {
echo "you are at right place ";
}
calling it in ajax url -:
post_data = $("#contact_us_form").serialize();
//Ajax post data to server
$.post('contact_us_receiver', post_data, function(response){
//load json data from server and output message
if(response.type == 'error')
{
output = '<div class="alert alert-danger">'+response.text+'</div>';
}else{
output = '<div class=" alert alert-success">'+response.text+'</div>';
$('#contact_us_form input ').val('');
$('#contact_us_form select ').val('');
$('#contact_us_form textarea ').val('');
}
$('#result').html(output);
$('#contact_us_form input ').css('border','');
$('#contact_us_form select ').css('border','');
}, 'json');
But it is giving me 404 not found error in console . please advise me how can i make call to my function .
Currently you are sending your request to an url named
contact_us_receiver
, which probably does not exists and hence returns the 404.In wordpress all ajax request run against the same URL. The specific action is then transmitted using the
action
parameter.For more details and working examples have a look at the respective Codex article: