How to catch NetworkError in JavaScript?

前端 未结 4 647
失恋的感觉
失恋的感觉 2021-02-07 01:30

In Chrome\'s JavaScript console, if I run this:

var that = new XMLHttpRequest();
that.open(\'GET\', \'http://this_is_a_bad_url.com\', false);
that.send();
         


        
4条回答
  •  旧时难觅i
    2021-02-07 01:47

    I think what you meant was:

    if(exception.name == 'NetworkError'){
       console.log('There was a network error.');
    }
    

    I don't believe the Error is an instance of NetworkError, but thrown in this manner:

    throw {
       name: 'NetworkError',
       message: 'A network error occurred.'
       // etc...
    }
    

提交回复
热议问题