admin管理员组

文章数量:1391999

XMLHttpRequest.status is typically just '0' if some network type error has occurred trying to make an Ajax/XMLHttpRequest call. But surely there should be a way of telling the user exactly what the network error was, e.g. whether the DNS resolution failed, or the connection was actively refused, or even whether the connection was made but then aborted before any valid HTTP response was sent? I.E. at least prints out some extra info in the JavaScript console, e.g.

SCRIPT7002: XMLHttpRequest: Network Error 0x2efd, Could not plete the operation due to error 00002efd.

But it's hardly very friendly, and I can't see how to query that info from javascript to translate into something I can show to the user.

I don't mind if it has to be implemented differently for different browsers, but I'm struggling to see any way of fetching in this information at all currently.

BTW at a minimum I'd want to be able to distinguish:

  • a) no internet connectivity (e.g. wifi disconnected)
  • b) DNS failure (can't resolve the name)
  • c) couldn't establish TCP connection to server (either actively refused or timed out)
  • d) invalid response (not an HTTP response)
  • e) SSL certificate failure

In any case where a valid HTTP response IS returned, but is not status 200 OK, it's fine - I can get a sensible error message to display to the user. But for anything else there doesn't seem to be a way to distinguish the various possible types of errors. In my application the user is responsible for supplying the address to make the Ajax requests to, so if they get it wrong I want to be able to tell them why the request is failing.

XMLHttpRequest.status is typically just '0' if some network type error has occurred trying to make an Ajax/XMLHttpRequest call. But surely there should be a way of telling the user exactly what the network error was, e.g. whether the DNS resolution failed, or the connection was actively refused, or even whether the connection was made but then aborted before any valid HTTP response was sent? I.E. at least prints out some extra info in the JavaScript console, e.g.

SCRIPT7002: XMLHttpRequest: Network Error 0x2efd, Could not plete the operation due to error 00002efd.

But it's hardly very friendly, and I can't see how to query that info from javascript to translate into something I can show to the user.

I don't mind if it has to be implemented differently for different browsers, but I'm struggling to see any way of fetching in this information at all currently.

BTW at a minimum I'd want to be able to distinguish:

  • a) no internet connectivity (e.g. wifi disconnected)
  • b) DNS failure (can't resolve the name)
  • c) couldn't establish TCP connection to server (either actively refused or timed out)
  • d) invalid response (not an HTTP response)
  • e) SSL certificate failure

In any case where a valid HTTP response IS returned, but is not status 200 OK, it's fine - I can get a sensible error message to display to the user. But for anything else there doesn't seem to be a way to distinguish the various possible types of errors. In my application the user is responsible for supplying the address to make the Ajax requests to, so if they get it wrong I want to be able to tell them why the request is failing.

Share Improve this question edited Mar 18, 2015 at 0:41 Dylan Nicholson asked Mar 18, 2015 at 0:07 Dylan NicholsonDylan Nicholson 1,33515 silver badges20 bronze badges 3
  • you can get tables of HTTP codes, the rest should e from your server. or a table of browser codes... – dandavis Commented Mar 18, 2015 at 0:09
  • I'm not talking about HTTP codes, I'm talking about when no HTTP response is returned. Could be a network error, or an SSL error, or some other configuration error. From reading the spec it seems that if used in synchronous mode a NetworkError exception would be thrown which might at least then provide more detail, but I can't see how to get hold of it in asynchronous mode. – Dylan Nicholson Commented Mar 18, 2015 at 0:17
  • 4 sadly, you can't get much error info from ajax itself. you're really asking 5 questions, how to determine each bullet. A: if(!navigator.onLine) alert("no net"); B. ajax location.href or "/" to determine (provided net works). C. xhr.onloadend/onerror can catch, D. should look like a typical CORS error, E. will show a nasty scary message to the user for you, or look like a CORS error... – dandavis Commented Mar 18, 2015 at 0:51
Add a ment  | 

2 Answers 2

Reset to default 2

Handling onerror of XHR helps you more here with the actual details that what has gone wrong; you can as well try ontimeout handler to catch specifically if it is getting timed out; and also as mentioned by @dandavis before XHR you can as well check for navigator.onLine to avoid any no network situation before calling XHR. A simple stub here:

function makeRequest() {
    if(navigator.onLine){
    let xhr = new XMLHttpRequest();
    xhr.open('GET', 'someurl/somepage', true);
    xhr.timeout = 2000;
    xhr.onload = function () {
        // Your request is pleted
        if (xhr.readyState == 4 && xhr.status == 200) {
            //successful
        }
    };
    x.onerror= function(e) {
    //Error occured, e will give you more information
    };
    xhr.ontimeout = function (e) {
        // Your request timed out
    };
    xhr.send(null);
  }
  else{
    alert('No network connection');
  }
}

You'd better dig into the onerror event of XMLHttpRequest instance

xhr.onerror = function(e) {
   if(e matches stuation 1) {
      console.log('no internet connectivity (e.g. wifi disconnected)');
   } else {
      //...other conditions
   }
};

It may somehow difficult to judge what the exact type of error occurs, just try check e.message or something else.

Notice that a status code != 200 will not trigger the onerror event but the onload event. handle it carefully

本文标签: javascriptHow do display actual network error to user when using XMLHttpRequestStack Overflow