1

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:

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.

4
  • when I put the same code in my functions.php and run jQuery.post('/wp-admin/admin-ajax.php',{action:'my_ajax_foo'},function(response){console.log(response);}); in Chrome's console (which is just like firebug), it works exactly as expected. I would first try doing that to remove all the other variables. If that still doesn't work, my hunch would be that something is interfering with the request (could be a plugin, htaccess, etc) Commented Feb 12, 2012 at 4:02
  • After posting my comment, I thought "well, duh, I'll just open admin-ajax.php and see what would redirect the request", and you know what I found? Nothing. So I'd bet top dollar it's a plugin. Do you have any plugins which restricts access? Anything with user roles, site members, etc? Commented Feb 12, 2012 at 4:10
  • What happens when you visit the admin-ajax with your action as a GET variable: domain.com/wp-admin/… Commented Feb 12, 2012 at 8:29
  • How about using a relative path instead of an absolute path? Commented Feb 14, 2012 at 22:30

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.