• Hi,
    I’m working on local host Linux machine.
    I’m having a strange problem; until yesterday before I updated WordPress to the 5.8.1 version I din’t have any problem with ajax for non privileged user, it worked fine.
    The logged-in user still work fine today…
    Now any non-privilege ajax call return an error.
    to do the ajax call I use Jquery:

    function myJavascripFunction(){
    	var foo = 'foo';
    	jQuery(document).ready( function($) {
    		$.ajax( {
    			method : 'POST',
    			dataType : 'json',
    			url : my_var.ajaxurl,
    			data : {
    				foo : foo,
    				_wpnonce : my_var.nonce,
    				action : 'my_php_ajax_function'
    			}
    		} )
    		.done(
    			function( data ){
    				console.log(data);
    
    				
    			}
    		);
    	} );
    }

    on the php side I use a function that looks like this:

    add_action( 'wp_ajax_nopriv_my_php_ajax_function', 'my_php_ajax_function' );
    
    function my_php_ajax_function(){
    	if ( wp_verify_nonce( $_POST['_wpnonce'], 'wp_rest' ) ){
    
    			echo json_encode(
    				array(
    					'foo' => $_POST['foo']
    				)
    			);
    			exit;
    
    	} else {
    		exit;
    	}
    }

    Does anyone has the same problem?
    Any Idea?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    I’ve not encountered any trouble. How are you passing my_var data from PHP?

    Echo something different for the else condition of my_php_ajax_function() to see if your callback executes and that there is a nonce issue.

    Thread Starter antonop4u

    (@antonop4u)

    yes through the function:

    wp_localize_script( 'my_general_functions_js', 'my_var', array(
    		'ajaxurl' => admin_url( 'admin-ajax.php' ),
    		'nonce' => wp_create_nonce( 'wp_rest' )
    	) );

    I also tried to echo something different in the else just at string:

    echo json_encode(
    	array(
    		'foo' => 'foo'
    	)
    );

    but it still gives me an error 400;
    If I checked the response in the network section of the developer tool it just shows a 0, I presume it’s a false;
    The strange thing is that until the update everything was fine, it actually it is still fine for the logged in user. The logged in user ajax call work perfectly.

    Moderator bcworkz

    (@bcworkz)

    Thanks, that all seems in order. After filling in the gaps like enqueue script, which we must be doing the same if logged in works, your nopriv code works OK on my site (latest version). There must be something about differing server configuration at play, like firewall or modSecurity configuration. It’s not unheard of for admin-ajax.php to be locked down tighter than it should be. Doesn’t explain failure after update, but perhaps it was coincidental.

    Ajax coding errors usually come back as 500 status. 400 status usually indicates security or path issues.

    Thread Starter antonop4u

    (@antonop4u)

    If I’m the only one with this problem it must be something on my code/server/setting… I also agree it must be a coincidence that this is happening after the update.
    Thanks anyway

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘No privilege ajax not working after WordPress update’ is closed to new replies.