miszczu
miszczu

Reputation: 1189

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.

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

Upvotes: 1

Views: 1440

Answers (3)

IndieTech Solutions
IndieTech Solutions

Reputation: 2541

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,

Upvotes: 1

Prince Singh
Prince Singh

Reputation: 5183

Replace 
 url: "<?php echo get_permalink(11); ?>",
with
url: '<?php echo admin_url('admin-ajax.php'); ?>'

also you must pass an 'action' param in your data which can then be attached to callback function

must see this :

http://codex.wordpress.org/AJAX_in_Plugins

Upvotes: 0

superphonic
superphonic

Reputation: 8074

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..

Upvotes: 0

Related Questions