简体   繁体   English

如何检查错误是否是jQuery中的#403?

[英]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 . 我想做的是检查错误代码是否为#403 ,如果是,则打印出growlMessage Could someone explain me how to do so? 有人可以解释我该怎么做吗?

Here's my jQuery function: 这是我的jQuery函数:

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 我在您的代码中看不到任何Ajax调用,如果您想检查错误代码,可以使用statusCode

Use statusCode like : 使用statusCode像:

$.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:的选项$.ajax ,如

error: fileInputError,

the first argument is a jqXHR object. 第一个参数是jqXHR对象。 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.');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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