404 error in ajax call, wordpress

I’m using ajax to collect data from enquiry form, then data is send to another page, and this page supposed to send this data to my email.
The problem is when I click send button, I’m getting 404 error in firebug console.
In template (with ajax code) I use this call:

$.ajax({
        type: "POST",
        url: "<?php echo get_permalink(11); ?>",
        data: {
                name: $('.enquiryName').val(),
                email: $('.enquiryEmail').val(),
                comments: $('.enquiryComments').val()
        }
}).done(function(msg) {
    if (msg=='1') {
        alert('<strong>Your enquiry has been sent successfully.</strong>');
        $('.enquiryName').val('');
        $('.enquiryEmail').val('');
        $('.enquiryComments').val('');
    } else {
        $('.errorBox').html(msg);
    }
});

Target page is just another page created in wordpress, with very basic template. When I put to browser url bar this page address and press enter I get message Nothing to send., which is correct.

Read More

What might be wrong? In ajax I get 404 error, in browser it’s fine.

Related posts

Leave a Reply

3 comments

  1. You will need to get an instance of the Ajax URL in frontend.
    To do that you need to enqueue your script and use the WP function wp_localize_script

    so in your functions.php

    function enqueue_AjaxURL() {
    
        wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/jmy-Yourscript.js', array('jquery') );
    
        wp_localize_script( 'ajax-script', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
    }
    add_action( 'wp_enqueue_scripts', 'enqueue_AjaxURL' );
    

    and in your JS file you can use the object:
    url: ajax_object.ajax_url,

  2. You need to use a relative URL in an Ajax call… replace <?php echo get_permalink(11); ?> with the actual relative URL and see if it works..