1

I know this has been asked 1000 times already, but I'm not seeing anything wrong in my code and am still getting a 400 error (XHR).

Here is the code from my plugin:

require_once( plugin_dir_path( __FILE__ ) . 'classes/duplicate_statement_controller.class.php' );

//add_action( 'plugins_loaded', array( 'UWSA_Calendar_Controller', 'get_instance' ) );

add_action( 'wp_enqueue_scripts', function(){
    wp_enqueue_style('duplicate-tax-css' , plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.css' );
    
    wp_enqueue_script( 'duplicate-tax-js', plugin_dir_url( __FILE__ ) . '/assets/duplicate-tax.js', array('jquery'), '1.0', true );
    
    wp_localize_script( 'duplicate-tax-js', 'tax_object',
        array(
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce' => wp_create_nonce('tax-nonce')
        )
    );
} );

My jQuery AJAX call:

$.ajax({
    url: tax_object.ajax_url,
    type: 'post',
    data: {
        action: 'add_row',
        nonce: tax_object.nonce
    },
  success: function (response) {
    // probably re-calc here
    console.log(response);
  }
});

And the code I want to run:

private function __construct() {
            //require_once(  plugin_dir_path( __FILE__ ) . 'duplicate-tax-manage-rows.class.php' );
            //require_once(  plugin_dir_path( __FILE__ ) . 'duplicate-tax-calc.class.php' );
            
            //Wordpress hooks and filters
            add_action( 'init', array ( $this , 'action_init' ) );

            // Ajax actions for rows
            add_action('wp_ajax_add_row',  array( $this , 'add_row' ) );
            add_action('wp_ajax_nopriv_add_row',  array( $this , 'add_row' ) );
            add_action('wp_ajax_remove_row',  array( $this , 'remove_row' ) );
            add_action('wp_ajax_nopriv_remove_row',  array( $this , 'remove_row' ) );
        }
        
        
        public function add_row() {
            error_log("!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        }

Every time I run this, I get a 400 error (strict-origin-when-cross-origin).

I just don't see anything wrong. Does anyone see something I'm not?

To clarify some, I have registered the AJAX calls, put them in wp_enqueue and wp_localize_script. Also make calls with wp_ajax_nopriv as suggested elsewhere. As far as I can tell, all the syntax is correct.

This code is even used in another plugin without error so I'm not sure if this is a problem with the code or a setting somewhere else.

Please, I really need help.

4
  • 1
    is the website and xhr request calling the same domain? Commented Nov 6 at 23:50
  • "I get a 400 error (strict-origin-when-cross-origin)" - that's not one single error, that's two. But it might be that the one is causing the other - that your system does not send the CORS headers it normally would, because it ran into a condition that triggered the 400. What do you see, when you inspect the response body using your browser dev tools, any more enlightening information in there perhaps? Commented Nov 7 at 6:59
  • @unixmiah - yes. It's calling the same domain and I'm using the full URL as recommended in the docs. Commented Nov 7 at 15:45
  • @C3roe - I'm not sure what you're looking for, but these are the response headers: Status 400 VersionHTTP/2 Transferred558 B (1 B size) Referrer Policystrict-origin-when-cross-origin Request PriorityHighest DNS ResolutionSystem The response body is just 0. Commented Nov 7 at 15:52

1 Answer 1

1

When you work with Ajax and you have a class-based backend like this, you should always make sure that the methods that are involved are not private.

Also, make sure that the class instance is being created and the constructor is called in the correct place

You can use the private methods inside the Ajax callback method, but the constructor and the Ajax callback method should be public.

So, for now, please change the private to public on the __construct

Wrong:

private function __construct() {

Correct:

public function __construct() {

If it didn't help, then it is for sure a problem with how you instantiate your class. Share more code so I can help.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, Mohammad. I made that change and am still getting the error. As I mentioned, this code is used in another plugin and it works fine there so it makes me wonder if this could be a Wordpress or server setting I'm missing. All of the code is pasted above so I don't have anything more to share.
sorry, my mistake. There was one line of code missing from above, where I include the classes. I've reposted it above.
Thanks, Mohammad. I recoded how I instantiate this and now it's working.

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.