Support » Developing with WordPress » WordPress Ajax call not working in Firefox

  • Resolved Eric Malalel

    (@teachlynx)


    0
    down vote
    favorite
    I have set up different AJAX calls in my WP site and 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 [removed from public forum]

    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.

    All these buttons trigger AJAX calls on the server, and an alert message is displayed if some validation went wrong on the server.

    All these buttons work fine and you will see the alert message on any browser, including Firefox.

    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.

    First, 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 code in the “functions.php” of my WordPress child theme:

    
    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.

    I have cleared the cache of Firefox but it does not help.

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

    • This topic was modified 3 years ago by James Huff.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    First of all, change those credentials immediately. This is a public forum.

    Next, try adding define('CONCATENATE_SCRIPTS', false); to your wp-config.php file just below the define('DB_HOST' line.

    To do so, access your server via SFTP or FTP, or a file manager in your hosting account’s control panel, and edit the file with a plain text editor.

    https://codex.wordpress.org/Editing_wp-config.php#Disable_Javascript_Concatenation

    We won’t be logging into your site for anything, it’s against policy here. If you need someone to do direct work, please try http://jobs.wordpress.net/ or https://jetpack.pro/ and do not accept any hire or direct access offers posted to these forums.

    • This reply was modified 3 years ago by James Huff.
    Eric Malalel

    (@teachlynx)

    Hi and thanks for quick feedback.

    I just did it, restart the web server to be sure, but sorry, no change, so I removed it.

    Moderator James Huff

    (@macmanx)

    Volunteer Moderator

    Ok, let’s rule-out some third-party elements next.

    Try deactivating all plugins. If that resolves the issue, reactivate each one individually until you find the cause.

    If that does not resolve the issue, try switching to the Twenty Sixteen theme to rule-out a theme-specific issue (theme functions can interfere like plugins).

    Moderator bcworkz

    (@bcworkz)

    If James’ tests don’t unveil anything, how are you loading your script and jQuery? You should be using the WP version within the installation and not an external location like googleapis.com. This is best accomplished with wp_enqueue_script() and specifying ‘jquery’ as a dependency. Your script will then not be able to use the $ shortcut unless you use a noConflict wrapper.

    Eric Malalel

    (@teachlynx)

    I have deactivated all plugins, no change.
    I cannot switch to Twenty Sixteen as I rely heavily on the Divi theme from Elegant Theme and all the JQuery stuff is implemented in a child theme.
    Here is how I load my jQuery, which is needed only by custom post types:

    
    	if ( get_post_type() == 'kpcms_place' ) {
    		// JQuery scripts and JQuery with WP Ajax URL set 
    		wp_enqueue_script( 'kpcms-KPS-pages', get_stylesheet_directory_uri() . '/js/kpcms-KPS-pages.js', array( 'jquery' ), '1.0.0', true );
    		// pass Ajax Url to script.js
    		wp_localize_script('kpcms-KPS-pages', 'WP_AJAX_URL', admin_url( 'admin-ajax.php' ) );
    	}
    

    Please notice the jQuery is executed, since the user is redirected to the home page.
    But it is not disconnected.
    And when I look at network trafic, I don’t see the Ajax call in Firefox, while I can see it in CHrome and IE.

    I have followed the following tutorial:
    https://premium.wpmudev.org/blog/using-ajax-with-wordpress/

    Moderator bcworkz

    (@bcworkz)

    Thanks for the info, that all looks fine. Have you checked the FF console for JS errors after the AJAX call is supposed to occur? Is your FF version up to date?

    If there’s nothing in the console of merit, then the theme has become highly suspect and the only way to rule it out is to isolate the relevant code and copy it into the twentysixteen theme, then activate it. Keep a clean backup to restore to after testing.

    Eric Malalel

    (@teachlynx)

    Well, in fact, this code
    window.location.href = $kps_Host + $kps_LoginPath;
    needed to be put in the AJAX post callback, to guaranty the AJAX call is executed before the page navigation takes place.
    I changed my code and it works fine now.

    
    $("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;
    });
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘WordPress Ajax call not working in Firefox’ is closed to new replies.