javascript - Detect specific error (403) with JS - Stack Overflow

I'm attempting to catch a potential error on my page, the page has a collection of URLs (retrieved

I'm attempting to catch a potential error on my page, the page has a collection of URLs (retrieved at page load) to various resources which are loaded when needed.

However these resources are only available for a set time, so if the user leaves the page idle for a long period, the URLs may no longer be valid and may return a 403.

I'm currently detecting errors on the resources using something along the lines of:

$(".identifier").on("error", function () {
    console.log("Error on resource load");
});

But can I detect the specific type of error / the error code? e.g. the 403 permissions error rather than a generic "There is an error". If possible I would like to be able to react differently to different errors.

I'm attempting to catch a potential error on my page, the page has a collection of URLs (retrieved at page load) to various resources which are loaded when needed.

However these resources are only available for a set time, so if the user leaves the page idle for a long period, the URLs may no longer be valid and may return a 403.

I'm currently detecting errors on the resources using something along the lines of:

$(".identifier").on("error", function () {
    console.log("Error on resource load");
});

But can I detect the specific type of error / the error code? e.g. the 403 permissions error rather than a generic "There is an error". If possible I would like to be able to react differently to different errors.

Share Improve this question asked May 20, 2015 at 11:19 DBSDBS 10k4 gold badges38 silver badges58 bronze badges 1
  • Maybe this can help you stackoverflow./a/10556743/1438764 – David Chavez Commented May 20, 2015 at 11:28
Add a ment  | 

1 Answer 1

Reset to default 4

Let's say you have all available URL's to your resources inside an array e.g.

var resources = [
    "http://url/to/resource-one.fiel",
    "http://url/to/resource-two.fiel"
    ]

Then you could use the following function to check the http response of these resources, by looping over the array and passing each resource to checkResources.

   function checkResource (url, index) {
      var req = new XMLHttpRequest();
      req.open('HEAD', url, true);
      req.send();
      if (req.status === 404) {
        return index;
      }
      if (req.status === 403) {
        return index;
      }
   };

Or use a specific URL without a loop and delete the index param in the function, maybe return the URL instead. In other words do an ajax HEAD request, which will only return the headers resource, rather than the resource itself.

You could as well get all the header information or just a specific header info by name like so

 xmlhttp.open('HEAD', 'path/to/your/resource', true);
 xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
      console.log(xmlhttp.getAllResponseHeaders()); // get all header info
      console.log(xmlhttp.getResponseHeader("Last-Modified")); // get by name
    }
 }
 xmlhttp.send(null)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744188730a4562331.html

相关推荐

  • javascript - Detect specific error (403) with JS - Stack Overflow

    I'm attempting to catch a potential error on my page, the page has a collection of URLs (retrieved

    9天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信