Home > plugins > wp_ajax action responds with 0

wp_ajax action responds with 0

September 23Hits:1
Advertisement

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?

Answers

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

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.

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.

Related Articles

  • wp_ajax action responds with 0September 23

    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', 'upd

  • How return normal search result using wp_ajax action for wordpressOctober 23

    I want the normal search result to be available when using the wp_ajax action. Is there a way to do execute the normal WP_Query for the parameters that are available in the current request? This is my current code: add_action('wp_ajax_nopriv_update_s

  • How can I pass a variable to wp_ajax action?April 14

    I'm writing a plugin that pulls data from an external API and inserts it into a bbPress topic. Here's some sample code: function bbp_ajax() { $topic_id = bbp_get_topic_id(); $the_issue_key = get_post_meta( $topic_id, 'bbp_jira_issue_field', true); $n

  • wp_ajax action is not runningApril 22

    I'm writing a plugin. I want to run the method get_my_option when someone pressing a button in the settings page of my wordpress plugin. The ajax call is made but the method never runs. In the page I have the following code: <div class="wrap"

  • wp_ajax action is not run when ajax triggerNovember 8

    I'm trying to implement the ajax in wordpress world but now stuck. Function hook suppose to be called when ajax triggered is not happening. I check the network admin-ajax.php is called but response is null implied that getsomething function is not ca

  • Adding wp_ajax to a theme outside functions.php (on specific template page)April 26

    I'm doing a function which requires the use of the wp_ajax action. I've followed this guide to set it up with the differences of adding the requesting the object AJAX file in functions.php and also added if(isset($_REQUEST['action'])) { do_action('wp

  • Should one declare alternative response types (e.g. JSON) in Rails controller actions even if not utilising them?January 11

    Just wondering what the accepted convention is for Rails controller design. Currently, every controller in my app that I've written it set up to send a JSON response when necessary. Thing is, I only ever utilse HTML responses in my app. So is it a go

  • How to declare a JS variable in an AJAX callDecember 10

    I'm using: wp_localize_script( $handle, $namespace, $variables ); to declare some variables before the initial AJAX call is made, but I imagine I can't do the same thing again within a callback function? I need to declare a newly created ID for use i

  • grab or load text on demandFebruary 1

    What i want to do I want some content (could be stored any where in a post or custom field) to load only when the user clicks for it to load. This content could be any thing (text,images,html etc) and this may be accomplished through any way (eg ajax

  • Safari extension send message to a specific tabMarch 26

    Is there a way to send a message from the global page to a specific tab? What I'm currently doing is, on tab creation the injected script creates a unique id and sends a message with this number to the global page and the global page saves this numbe

  • How to Custom Edit Post Title & Permalink Slug?June 7

    I'm trying to build some functionality where if you're logged into an admin account you'll see a small 'edit' link next to each post title. clicking will use jquery to create 2 form inputs - 1 with the current post title and the other containing the

  • $wpdb- update has finished saving trigger?July 26

    I have a custom table that I am updating via wp_ajax action. I am inserting a row and inserting all of the values other than id which is an AUTO_INCREMENT. I have an ajax response that creates a new row on the page with the new data. In my response I

  • How much current would you design for the various supplies required for Spartan 6 LX9 devices?August 16

    I'm designing a new board based on XC6SLX9-3TQG144C. There are three supplies required -- VCCINT, VCCAUX, and VCCO. VCCINT is 1.2V and the other two will be 3.3V. I'm wondering how much current to design for in the power supplies. Obviously the curre

  • Update meta values with AJAXApril 7

    I am working on a voting system in Wordpress. I first made it with GET requests but someone told me you should not use GET for this, So I started working with AJAX to update custom fields/meta values, after reading this article. I've read a lot of th

  • Accessing $post after PHP is executedNovember 5

    I am using javascript (angular.js) to make page animated page transitions in a wordpress site. I am not experienced enough to have the site completely built in angular and then display posts etc using angular (JSON? etc) to access the php. Rather I h

  • ROR pass params to modalJanuary 20

    I'm trying to open a modal that displays a list of items. The list of items is already available in the view as a variable. How do I pass this variable to the modal? Below is roughly how the main view looks like: View.html.erb <div> <%= @users.ea

  • Select2 Ajax not Calling wp_ajax_ in Wordpress (Javascript error, not a WP error)January 22

    I have tried everything I can think of, but I am not able to get Select2 Ajax to call the Wordpress wp_ajax action. Here is my functions.php (excluding the enqueueing and the localizing of the script): function get_list_posts() { $search = sanitize_t

  • Should I use `respond_to` or have a dedicated API namespace in Rails?February 6

    In a Rails web app it's common to have some actions in your controller return JSON as opposed to an HTML view. This is especially common if you are using many Javascript components that require JSON responses. I was writing such an action the other d

  • PHP framework for ADR (Action Domain Responder) PatternMay 2

    I just got to know about ADR (Action Domain Responder) pattern that seems to be much more efficient than MVC (Model View Controller), but as oppose to MVC, I have it difficult to find a framework for ADR. The reason must be its age, it is so young. B

  • Eclipse stops responding to most keyboard input after some amount of time/specific actionsAugust 1

    For some time now, it seems like Eclipse will stop responding to keystrokes (via any entry point of the app -- text editor, menus, etc...) if I switch between workspaces too many times. In some cases, it will resume responding after a bit of time. I'

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.318 s.