Ajax call to php script returns 404 error Ajax call to php script returns 404 error wordpress wordpress

Ajax call to php script returns 404 error


Since the server sends a 404 (for god knows what reason), there are two ways to fix/circumvent this:

  1. Ignore the HTTP response code and change success to complete in the jQuery ajax call, so that the handler is executed when the request is done no matter the server response. You know the server response (it always works). The HTML should still be available in the jQuery complete handler.
  2. Overwrite the 404 that something sends on the server (probably something Wordpress) by executing (before printing any output): header('HTTP/1.1 200 OK'). Since the script is executed, this will overwrite the crazy 404 and jQuery will receive that 200 and execute the success handler.

You could try both =) I'm pretty sure the first one will work (but that's not so clean). I'm also pretty sure the 2nd will work, but I don't know Wordpress well enough to make promises =)


I'm guessing it's because Wordpress already has an AJAX mechanism built in and it stops you from implementing it on your own. This page explains how to add AJAX to plugins:

http://codex.wordpress.org/AJAX_in_Plugins

Here's a snippet from the page:


Ajax on the Administration Side

Since Ajax is already built into the core WordPress administration screens, adding more administration-side Ajax functionality to your plugin is fairly straightforward, and this section describes how to do it.

Here's a short example. All this will be in one file.

First, add some javascript that will trigger the AJAX request:

<?phpadd_action('admin_print_scripts', 'my_action_javascript');function my_action_javascript() {?><script type="text/javascript" >jQuery(document).ready(function($) {    var data = {        action: 'my_action',        whatever: 1234    };    // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php    $.post(ajaxurl, data, function(response) {        alert('Got this from the server: ' + response);    });});</script><?php}

Then, set up a PHP function that will handle that request:

<?php     add_action('wp_ajax_my_action', 'my_action_callback');    function my_action_callback() {    global $wpdb; // this is how you get access to the database    $whatever = intval( $_POST['whatever'] );    $whatever += 10;    echo $whatever;    die(); // this is required to return a proper result}

That's it! You will need to add a few details, such as error checking and verifying that the request came from the right place ( using check_ajax_referer() ), but hopefully the example above will be enough to get you started on your own administration-side Ajax plugin.NOTE: Since Version 2.8, The javascript global variable ajaxurl can be used in case you want to separate your javascript code from php files into javascript only files. This is true on the administration side only.


As seen here https://cooltrainer.org/fixing-false-404-headers-on-external-pages-including-wp-blog-header-php/ this solution tested and works well:

require_once("path/to/wp-config.php");$wp->init();$wp->parse_request(); $wp->query_posts();$wp->register_globals(); $wp->send_headers();