jQuery ajax call throws an HTTP 302

I’m trying to implement a jQuery Autocomplete where the Autocomplete gets its data from a ajax source. However, for some reason when I make the call it results in a 302 which in turn results in a 404.

This is what I have:

Read More

In functions.php:

function my_ajax_foo() {
  die("OK");
}

add_action('wp_ajax_my_ajax_foo', 'my_ajax_foo' );
add_action('wp_ajax_nopriv_my_ajax_foo', 'my_ajax_foo' );

My Javascript:

function onGetData(term, callback) {
  jQuery.post( "http://mydomain.com/wp-admin/admin-ajax.php", {
    action: "my_ajax_foo",
    cookie: encodeURIComponent(document.cookie),
    term: term
  },
  function( response ) {
    console.log( response );
  } );
}

jQuery(document).ready(function() {
  jQuery("#my_input_field").autocomplete( { source:onGetData } );
});

All the Javascript is loaded correctly. When I type in the Autocomplete input box the ajax call is made but I can see in Firebug that the call results in a HTTP 302.

When I call the http://mydomain.com/wp-admin/admin-ajax.php simply from the browser URL I get a -1 returned so I know that the URL exists.

Any help much appreciated.

Related posts

Leave a Reply

1 comment

  1. I’d say, just skip that:

     cookie: encodeURIComponent(document.cookie)
    

    There’s no need to post any cookies on XHR – the browser handles that.

    Well, how about something like that instead (untested):

    data:{'term':encodeURIComponent($.term)}
    

    Most easy is to use FireBug, open Net panel and inspect both HTTP headers.