Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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!

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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;
}
share|improve this answer
    
thank you for your answer. what do you mean by a non-permalink url? wp_redirect is good tip though. I searche all plugins for it and two active plugins are using it. However even if i deactivate them there are still some 301 fired and my browser crashes. Don't get it. Is there something like remove_all_filters() for redirects too? So i could simply get rid of each and every redirect, filter and action in my searchmap? –  mathiregister Mar 27 '11 at 12:59
    
non permalink url is like my_domain.com?page=3. Whichever your page number is. I cant remember there being a remove_all_redirects. What you can is turn the request from html to a normal ajax call that executes a function. that way you wont have to worry about anything. I'll add an example to my answer. –  Patriek Mar 27 '11 at 17:59
    
thank you for your help..a few question before I try to implement this. Why if(is_admin()) { ? The ajax request should work for all visitiors. Do I simply replace my jqXHR_Old = $.ajax({ with your jQuery.post( ? Do I add the add_action hooks in my functions.php file? –  mathiregister Mar 28 '11 at 8:39
    
THe admin is optional, but if i remember correctly all ajax calls run in admin mode. I use it like that for the front website and it works just fine. I think you need to rewrite your ajax to use the wp_methods for ajax, think you are using a jquery method. –  Patriek Mar 28 '11 at 8:59
    
since I've never worked with ajax before and had a lot of troubles setting up my ajax script this is way too much for me :) ... what are the add_action calls for? my_searchmap_function? What does it contain/do? I mean it can't be that hard, can it? I simply want to load and handle a generated wordpress page with jquery and it's ajax functions. I really appreciate your help. thanks again! –  mathiregister Mar 28 '11 at 9:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.