yanatan16 / nanoajax

An ajax library you need a microscope to see
MIT License
246 stars 42 forks source link

200 returned on net::ERR_CONNECTION_REFUSED #14

Closed kjk closed 8 years ago

kjk commented 8 years ago

This is Chrome 47.0.2526.106 (64-bit) on Mac.

When testing locally and when the server is not running, a request callback has code = 200 and empty responseText.

This issue is here:

  function cb(statusCode, responseText) {
    return function () {
      if (!called)
        callback(req.status || statusCode,
                 req.response || req.responseText || responseText,
                 req)
      called = true
    }

In my case req.status is 0 but is converted to 200 (statusCode) because it's called from: if (req.readyState === 4) success(). In console I see 'net::ERR_CONNECTION_REFUSED' error.

Not sure what cases are where req.status 0 should be converted to something else. It seems to me this should be reported as error i.e. req.status should be passed as is to the callback.

yanatan16 commented 8 years ago

Thanks for the report. req.status || statusCode is supposed to catch req.status == undefined, not req.status === 0. I'll patch it to req.status === undefined ? statusCode : req.status

yanatan16 commented 8 years ago

So this is patched in 82459292c72fba0838f46a5fed1c1274497309ed. Released as v0.4.1.

Note that nanoajax doesn't do much for you in this case, still giving you a callback with cb(0, "", req). Like other errors in nanoajax, the callback can check the falsyness of the status code.

kjk commented 8 years ago

Thanks, that will work for my use case.

yanatan16 commented 8 years ago

@kjk I was thinking about this again, and realized that nanoajax could call back with (0, "Error") instead of (0, ""), so thats now what happens in v0.4.3.

kjk commented 8 years ago

@yanatan16 sounds like a good idea, thanks!