navigator.onLine not always working

后端 未结 2 1310
情话喂你
情话喂你 2020-11-27 05:19

I\'m having an issue with the navigator.onLine property.

I\'m running a simple website from a local kiosk running on a WAMP.

On my laptop when I test this it

2条回答
  •  旧时难觅i
    2020-11-27 05:50

    As Danilo Valente pointed: navigator.onLine property is not trustable,

    But NOT every error in ajax response means you are disconnected from the internet! It may be an API error (403,500,404 ....)

    If you are using axios, you can distinguish these errors like this:

    axios.request(options).catch(function(error) {
      if (!error.response) {
        // network error (server is down or no internet)
      } else {
        // http status code
        const code = error.response.status
        // data from server while error
        const response = error.response.data
      }
    });
    

提交回复
热议问题