• Resolved jeffreykarbowski

    (@jeffreykarbowski)


    I am getting 400 errors when using the button on the backend. I am using this code:

    function acfe_buttons_ajax() {
        echo 'Hello World';
        die();
    }
    add_action('wp_ajax_acfe/fields/button', 'acfe_buttons_ajax');
    add_action('wp_ajax_nopriv_acfe/fields/button', 'financial_buttons_ajax');

    And it hits /wp-admin/admin-ajax.php just fine, with the correct params, it is returning a 400 error though. Any thoughts?

    Params:
    {"Form data":{"action":"acfe/fields/button","field_name":"send_invoice","field_key":"field_5d7109d301824","nonce":"d5b586c6f8","post_id":"40039"},"Request payload":{"EDITOR_CONFIG":{"text":"action=acfe%2Ffields%2Fbutton&field_name=send_invoice&field_key=field_5d7109d301824&nonce=d5b586c6f8&post_id=40039","mode":"text/html"}}}

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    HTTP 400 error on WordPress Ajax call is typically an error due to bad action declaration. In your case, I can see that you use 2 different callback functions acfe_buttons_ajax & financial_buttons_ajax. One (or both) are most likely undefined, that’s why you get the 400 error code.

    Here is the WordPress documentation on ajax: https://codex.www.ads-software.com/Plugin_API/Action_Reference/wp_ajax_(action)

    Have a nice day!

    Regards.

    Thread Starter jeffreykarbowski

    (@jeffreykarbowski)

    Thanks, that was an issue, but only because when I copied over the code here I mistakenly didn’t change both action function names. I did however figure out the issue with the 400 error, it was that I was including the function only on the post.php edit page, it also needed to be included while doing ajax. (|| wp_doing_ajax()), and I am now getting a response.

    I am now having issues with getting the response to the success callback.
    Here is the code I have:

    function acfe_buttons_ajax() {
    	ob_start();
    	echo 'Results';
    	$result = ob_get_contents();
        ob_end_clean();
        return $result;
        wp_die();
    }
    add_action('wp_ajax_acfe/fields/button', 'acfe_buttons_ajax');
    add_action('wp_ajax_nopriv_acfe/fields/button', 'acfe_buttons_ajax');

    and:

    
    acf.addAction('acfe/fields/button/ajax_success', function(response, $el){
    		alert(response);
    });

    The js does the alert, but just alerts “0”. Any thoughts?

    The .js is enqueued in functions.php like:

    function agency_enqueue($hook) {
        if ( 'post.php' != $hook ) {
            return;
        }
        wp_enqueue_script( 'agency_js', get_stylesheet_directory_uri() . '/js/admin-agency.js', array('jquery'), null, true );
    }
    add_action( 'admin_enqueue_scripts', 'agency_enqueue' );
    Thread Starter jeffreykarbowski

    (@jeffreykarbowski)

    Nevermind, I figured this out, thanks so much for you efforts and wonderful extension to ACF!

    Plugin Author Konrad Chmielewski

    (@hwk-fr)

    Hello,

    Yeah you need to echo 'Results'; and not return it.

    I’m glad you enjoy ACF Extended! BTW, if you would like to support me, feel free to write a review, it always helps! ??

    Have a nice day,

    Regards.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘ACFE Button Ajax 400 error’ is closed to new replies.