php ajax problem – weird 301 responses!

hey guys, I really really need your help. I have just no idea why that happens.

I’m trying to load a wordpress page with the jquery $ajax method. However my browser is crashing all the time when I try to load this page.

Read More

I’ve build a kind of ajax search that requests a wordpress page when typing inside of an input field. This is my code.

jqXHR_Old = $.ajax({
       url: "searchmap", // searchmap means mydomain.com/searchmap
       dataType: "html",
       success: function (data) { ...

The weird thing is: When i change the url to a static page like url: "searchmap.html" everything works fine. As soon as I choose the dynamically generated WordPress Template “searchmap” the ajax response throws “301 Moved Permanetly” messages.

This is my template that works just fine when I call it in the browser…

<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="<?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>

Any idea why Firebug or Fiddler responds with “301 Moved Permanently” errors when I load this page with $.ajax() ?

My browser keeps crashing, even though I abort each ajax request when typing again. So there are never happening multiple ajax requests.

$.ajax requests mydomain.com/searchmap and mydomain.com/searchmap requests the server for all pages, posts and cats.

Any ideas? I’m completely clueless!

Related posts

Leave a Reply

1 comment

  1. 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="<?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;
    }