简体   繁体   中英

How to check if the error is #403 in jQuery?

I'm printing growl message if the user doesn't have access to use this function.

But now my code prints it anytime (despite the file is too big or the user doesn't have an access)

What I wanna do is to check if the error code is #403 and if so, just then print that growlMessage . Could someone explain me how to do so?

Here's my jQuery function:

function fileInputError(event, data, msg) {
  setTimeout(function() {
     var nav = $('#nav-users');
     var progressBar = nav.find('.progress-bar');
     progressBar.removeClass('bg-success progress-bar-success')
        .addClass('bg-danger progress-bar-danger');
    progressBar.text('Error');  
  }, 110);

  growlMessage('You don't have permission to use this function.');
}

I don't see any ajax call in your code, if you want check the error code you can use statusCode

Use statusCode like :

$.ajax({
  statusCode: {
    403: function() {
      growlMessage('You don't have permission to use this function.');
    },
    500: function() {
      alert('500 status code! server error');
   }
 }
});

If this function is called from the error: option of $.ajax , eg

error: fileInputError,

the first argument is a jqXHR object. You can get the status code from that.

function fileInputError(jqxhr, textStatus, error) {
  setTimeout(function() {
     var nav = $('#nav-users');
     var progressBar = nav.find('.progress-bar');
     progressBar.removeClass('bg-success progress-bar-success')
        .addClass('bg-danger progress-bar-danger');
    progressBar.text('Error');  
  }, 110);
  if (jqxhr.status == 403) {
    growlMessage('You don't have permission to use this function.');
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM