0

I'm writing a plugin. There is a button, that triggers an ajax-request to an action I added in my plugin page:

Head of my plugin PHP file:

add_action('wp_ajax_update_nav_items', 'update_nav_items' );
add_action('wp_ajax_nopriv_update_nav_items', 'update_nav_items' );

wp_enqueue_script( 'addItemToNav', plugin_dir_url( __FILE__ ) . 'js/navFunctions.js', array( 'jquery', 'json2' ) );
wp_localize_script( 'addItemToNav', 'menuItems', array(
    // URL to wp-admin/admin-ajax.php to process the request
    'ajaxurl'          => admin_url( 'admin-ajax.php' ),
    // generate a nonce with a unique ID "myajax-post-comment-nonce"
    'postCommentNonce' => wp_create_nonce( 'update_nav_items-nonce' ),
    'action' => 'update_nav_items'
    )
);
function update_nav_items() { 
    // Testing stuff 
    $response = json_encode( array( 'response' => 'success', 'html' => 'some value' ) );
    ob_clean();
    print_r( $response );
    echo json_encode( $response );

    die();
}

My JavaScript which triggers:

var data = {
    action: 'update_nav_items',
    postCommentNonce : menuItems.postCommentNonce,
    menuitems: JSON.stringify(itemtest)
};

jQuery.ajax({ 
    url: menuItems.ajaxurl, 
    data: data,  
    type: 'post',
    dataType: 'json',
    success: function( response ) {
        console.log(response);
    }
}); 

In the browser console, the request is sent to the correct url, including following parameters:

action  update_nav_items
menuitems   {"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}}
postCommentNonce    0cee7325b3

(menuitems is just for testing with correct JSON)

What I get as response is 0, so there seems to be a problem with the action. I just don't find it. I already tried adding actions also in my functions.php, it's still not working.

Anyone have a clue?

  • Do you have other plugins activated or a theme other than a default twenty* theme? In this case I would edit the admin-ajax.php file directly, placing some var_dump()s to determine where the script exits. E.g. it would be helpful to know, whether admin_init get called or not. – David Sep 23 '14 at 10:14
0

Your AJAX Requests requires an JSON to be returned. But I think your function doesn't return valid JSON.

Haven't tested it, but this function should work:

function update_nav_items() { 
    // Testing stuff 
    $response = json_encode( array( 'response' => 'success', 'html' => 'some value' ) ); // first JSON encode
    ob_clean();

    die(json_encode( $response )); // second JSON encode
}

For testing it, you can call the URL manually and have a look on the returned string, whether it's valid JSON or not.

Regards, lippoliv

  • Tried it, but still getting 0 back :( – Hyque Sep 23 '14 at 10:16
  • What was displayed in your browser, if you open the AJAX-URL manually? – lippoliv Sep 23 '14 at 10:19
  • And why did you double-encode your Response as JSON? – lippoliv Sep 23 '14 at 10:20
  • Opening the AJAX-URL manually displays a 0. The double encoding is probably legacy because I tried a lot of stuff found in similar questions. – Hyque Sep 23 '14 at 10:28
  • I would not do double-json-Encoding – lippoliv Sep 23 '14 at 10:35
0

Change your wp_localize_script-Call to the right Action:

wp_localize_script( 'addItemToNav', 'menuItems', array(
    // URL to wp-admin/admin-ajax.php to process the request
    'ajaxurl'          => admin_url( 'admin-ajax.php' ),
    // generate a nonce with a unique ID "myajax-post-comment-nonce"
    'postCommentNonce' => wp_create_nonce( 'update_nav_items-nonce' ),
    'action' => 'wp_ajax_update_nav_items'
    )
);

As 'action' you currently Input the function Name, but that is wrong. Your registered Action to call the function has an different Name.

0

Please follow the code:

add_action( 'wp_ajax_add_myfunc', 'prefix_ajax_add_myfunc' );
add_action( 'wp_ajax_nopriv_add_myfunc', 'prefix_ajax_add_myfunc' );

function prefix_ajax_add_myfunc() {
    // Handle request then generate response using WP_Ajax_Response
}

and in your ajax call do this:

jQuery.post(
    ajaxurl, 
    {
        'action': 'add_myfunc',
        'data':   'foobarid'
    }, 
    function(response){
        alert('The server responded: ' + response);
    }
);

in the ajax call you'll call your function without prefix_ajax_. Only call by it's remaining. In that case it's add_myfunc. In the response it will send done if everything goes right. Else response will be 0 or -1.

Hope it will help. Thank you.

-1

Thanks for all the help, unfortunately I don't have enough time to figure out what isn't working (customer tapping feet). All suggestions did'nt work, so I'm going the "normal" (ugly) way of reading out $_POST.

Thanks for the efford.

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.