php ajax problem – weird 301 responses!

I’m guessing the page your calling executes an wp_redirect with a 301 response somewhere.
Dont really have an answer for you but maybe you can try the following. Ajax does not know what type of page your calling from and there might be some strange query_vars loaded that trigger a redirect.

  • Your using a permalink to load the page, can you try using a non-permalink url?
  • do a text-search on your plugin folder for wp_redirect to see if something comes up.

Ajax call action.

    var response;
jQuery.post(
    // see tip #1 for how we declare global javascript variables
    MyAjax.ajaxurl,
    {
        // here we declare the parameters to send along with the request
        // this means the following action hooks will be fired:
        // wp_ajax_nopriv_myajax-submit and wp_ajax_myajax-submit
        action : 'searchmap',

        // other parameters can be added along with "action"
        postID : MyAjax.postID,
        count : '16'
    },
    function( response ) {
        jQuery("#div-searchmap").html(response);
        jQuery("#div-searchmap").show();
    }

);

If you want to use wp_ajax you will need to add some more.

    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    // this hook is fired if the current viewer is not logged in
    if (isset($_GET['action'])) {
    do_action( 'wp_ajax_nopriv_' . $_GET['action'] );
    }
    // if logged in:
    if (isset($_POST['action'])) {
    do_action( 'wp_ajax_' . $_POST['action'] );
    }
        if(is_admin()) {
        add_action( 'wp_ajax_searchmap', 'my_searchmap_function' );
        } else {
        add_action( 'wp_ajax_nopriv_searchmap', 'my_searchmap_function' );
}

Im using POST variable for my ajax script so it alawys runs in admin, if you write the jquery script with GET you will need to call the norpriv function outside admin. (just figured this out now)

U might have to fix the script here and there, it’s just an offhand example.

the function has to return the data you need. For example:

function my_searchmap_function() {
// Start output buffer
ob_start();
?>
div>
    <h3>Pages</h3>
        <ul>
            <?php wp_list_pages('title_li=&depth=0&exclude="); ?>
        </ul>
    <h3>Posts</h3>
        <?php $first = 0;?>
        <ul>
        <?php
        $myposts = get_posts("numberposts=-1&offset=$first');
        foreach($myposts as $post) :
        ?>
        <li><a href="https://wordpress.stackexchange.com/questions/13192/<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
        <?php endforeach; ?>
        </ul>
    <h3>Categories</h3>
        <ul>
            <?php wp_list_categories('title_li=&orderby=name'); ?>
        </ul>
</div>  
<?php 

    $output = ob_get_contents();

    // Stop output buffer
    ob_end_clean();
    $response = json_encode($output);

    // response output
    header( "Content-Type: application/json" );
    echo $response;

    // IMPORTANT: don't forget to "exit"
    exit;
}