Adding a condition for success or error using jQuery ajax + WordPress

When using the WordPress “nonce” system to authenticate an AJAX request, a response of “-1” is returned if the authentication fails. Rather than doing something like this in every jQuery.ajax success function:

success: function(msg){
    if (msg == '-1')
        console.debug('error')
    else {
        // ...
    }
}

is it possible to somehow augment jQuery and add a condition for my application that error: rather than success: is fired when the message returned is “-1”? Obviously much cleaner than a bunch of the same if statements.

Read More

I realize the normal response is to have the controller return a 4xx header, but would rather not have the WordPress plugin change the default behaviour of the ajax handler and possibly mess up other WordPress plugins.

Thanks!

Related posts

Leave a Reply

1 comment