wordpress ajax call not working in firefox

I have followed the very good tutorial from Daniel Pataki to add AJAX calls to my WP site.

Everything works fine, except one specific AJAX call and only in Firefox.

I give below the details to reproduce the problem. It includes login credentials, but it is a WP test site and these are fake credentials, so don't worry, you can use it.

Go to https://responsive.knowledgeplaces.com

Login with credentials "aweb01" as userid and password.

Once connected, you will see different buttons. These are buttons for testing purposes.

For instance, you can click on the button "no lpId parameter", and you will see an error message.
This button triggers an AJAX call on the server, and an alert message is displayed if some validation went wrong on the server.
This button works fine and you will see the alert message on any browser.

There is another button "deconnexion" (logout) on the upper right side of the page.
When you click on this button, you are logged out from the site.
The button works fine on Chrome and IE, but not on Firefox.

Here is how it is implemented.

There is a javascript file with the following code:

$("a[href='/kps_logout']").on('click', function() {
		// check href symbolic URL
		jQuery.post(
			WP_AJAX_URL,
			{
				'action': 'kps_logout',
				'security': $("#kpcms-ajax-nonce").val()
			}
		);
		// redirect and deactivate default navigation
		window.location.href = $kps_Host + $kps_LoginPath;
		return false;
	});

basically, I use a symbolic '/kps_logout' URL, and this JQuery code maps a logout function on any href with this link.
on the server, I have this:

add_action( 'wp_ajax_kps_logout', 'kps_logout' );
add_action( 'wp_ajax_nopriv_kps_logout', 'kps_logout' );
function kps_logout() {
	// security check
	check_ajax_referer( 'kpcms-ajax-nonce', 'security' );
	// logout user and die
	wp_logout();
	die ();
}

Once again, works perfectly in Chrome and IE, not if Firefox.

If I activate the network dev tools, it sounds the AJAX call is not even performed in Firefox, while I can see it in Chrome and IE.

On the "no lpId parameter" button, I can see the AJAX call in all browsers, and it works fine on all browsers. And it is implemented exactly the same way.

So if you can help me on this, it would be great!

  • Nithin
    • Support Wizard

    Hi Luce Tancil,

    Hope you are doing good today. :slight_smile:

    It's odd that it doesn't work only on Firefox, to have a better understanding of how your website works, could you please enable support access, so that I give a closer look. You can grant access from WPMU DEV > Support > Support Access > Grant Access, or check this manual: http://premium.wpmudev.org/manuals/wpmu-dev-dashboard-enabling-staff-login/

    Please let us know once you enable access, so that we could get this sorted. Have a nice day. :slight_smile:

    Kind Regards,
    Nithin

  • Éric Malalel
    • The Incredible Code Injector

    I have just solved it.

    The function to process on return of the AJAX call was in the wrong place, and in Firefox, the redirection to the home page was made before the return of the AJAX call.
    So we were redirected to the home page but not logged out.

    Here is the right code, just in case:

    $("a[href='/kps_logout']").on('click', function() {
    		// check href symbolic URL
    		jQuery.post(
    			WP_AJAX_URL,
    			{
    				'action': 'kps_logout',
    				'security': $("#kpcms-ajax-nonce").val()
    			},
    			function(){
    				// redirect and deactivate default navigation
    				window.location.href = $kps_Host + $kps_LoginPath;
    			}
    		);
    		return false;
    	});

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.