问题描述:

I am developing a custom theme for a client in Wordpress.

I'm doing an AJAX call that retrieves some data on document load, but while this works in Chrome, it redirects on the homepage in Firefox and IE and continue looping the AJAX request.

Opening Firefox console, I can see this error:

uncaught exception: out of memory

And this warning:

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help http://xhr.spec.whatwg.org/

Here's my ajax file

$(document).ready(function(){

var container = $('#single-path-content');

var parentId = container.data('parent');

if(container.length > 0){

ajax_main(parentId, 0);

}

});

// fred_vars.ajaxurl is defined in functions.php a console.log of this variable is "http://fred.locl/wp-admin/admin-ajax.php"

function ajax_main(parentId, start){

$.ajax({

url: fred_vars.ajaxurl,

type: 'POST',

data: 'action=fred_subpath&parent_id='+parentId+'&start='+start,

beforeSend: function(){

// SOME jQuery code

},

success: function(results){

$('#single-path-content')

.html(results);

// SOME jQuery code

},

error: function(){

alert('Error! Please reload');

},

complete: function(){

// SOME jQuery code

}

});

}

Just before rendering the homepage inside the page template the browser shows the alert in error function.

I've found some possible solution, but nothing that fits my scenario.

网友答案:

The problem was inside a plugin that had the following code inside of it used for redirect non logged users to the home page.

function mksm_disable_subscriber_dashboard() {
    if ( is_admin() && ! current_user_can( 'edit_posts' ) ) :
        wp_redirect( esc_url( home_url( '/' ) ) );
        exit;
    endif;
}

The code restrict access also to the functions.php request for admin_url( 'admin-ajax.php' ) necessary to make AJAX working.

The solution was to modify the code like this:

function mksm_disable_subscriber_dashboard() {
    if ( ( is_admin() && ! current_user_can( 'edit_posts' ) ) && ! defined( 'DOING_AJAX' ) ) :
        wp_redirect( esc_url( home_url( '/' ) ) );
        exit;
    endif;
}
相关阅读:
Top