In functions.php I have:

function testajax() {
echo "test";

add_action('wp_ajax_testthisajax', 'testajax');
add_action('wp_ajax_nopriv_testthisajax', 'testajax');

In scripts.js I have:

$("#thisformsid").submit( function() {
            url: CustomAjax.ajaxurl,
            type: "POST",
            data: {
                action: 'testthisajax'
            success: function(result){
                    console.log('success: ', result);
                error: function(xhr){
                complete: function(result){
                    console.log('complete: ', result);

My form is using the correct id and I can reach the ajax submit in my scripts.js file

The url is defined in scripts.js like:

var CustomAjax = {
    ajaxurl: "/wp-admin/admin-ajax.php"

However this fails. In console I see an error and the value "0" returned. ANy ideas why this would not work? It seems like this, as basic as it is, should not error out but I don't see the problem. Any ideas?

Update: In console I see that the error is labeled as being on line 18 of my install of jquery (it's minified so i don't know what is actually at that line)? Why would there be an issue with jquery?


First try to perform this without JS altogether. Just visit link yoursite.com/wp-admin/admin-ajax.php?action=testthisajax.

0 seems like something kinda works, but not quite. If I remember right failed requests should be -1.

  • This works as expected. Any ideas on where to go from here? – gaoshan88 Sep 15 '11 at 22:07
  • @gaoshan88 not sure... Are you using proper noConflict wrapper around your jQuery code? – Rarst Sep 15 '11 at 22:56
  • Well, I tried with both $(...) and jQuery(...). Would that be insufficient? – gaoshan88 Sep 15 '11 at 23:04
  • There's noConflict(); to be used inside and on top of the jQuery function. Then there's function($) { etc. so you can use $ savely inside the function. But the function always has to start with jQuery. – kaiser Sep 16 '11 at 0:08

Have you enqueued jquery-form? Also look for dependencies. If you look at the 3rd parameter, then you'll see that you can set them. So there should be jquery enqueued and registered first, then jquery-form and then your custom stuff.

  • jquery form... I am not doing anything with that. Is it necessary? What role does it play? – gaoshan88 Sep 16 '11 at 0:47
  • No, it's not necessary. Only if you want to pass pre-post or past-post data alongside your post data - it's just an easy & nice way to do this. – kaiser Sep 16 '11 at 1:22

Try defining your CustomAjax url using admin_url( 'admin-ajax.php' )

<?php print("var CustomAjax = '" . admin_url( 'admin-ajax.php' ) . "';\n");?>

Even better would be to pass the AjaxUrl using wp_localize_script:

wp_enqueue_script( 'scripts', get_bloginfo( 'stylesheet_directory' ) . '/js/scripts.js', array( 'jquery', 'jquery-ui' ), false);
wp_localize_script( 'scripts', 'CustomAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );

I tried your code, and it works fine - I get "success:test" on my domain.

But, I managed to get "0" - by changing ajax url from: mydomain.com/wp-admin/admin-ajax.php to www.mydomain.com/wp-admin/admin-ajax.php

Using ajax, you can make requests to the same domain only. Is it so in your case?


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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