I'm trying to build an interceptor to handle 401 requests, but even in the subscribe error function handler the status is 0. I tried setting observe: response but that didn't help.
I force my backend to add Access-Control-Allow-Origin header and verify the response.
After this the message No Access-Control-Allow-Origin is not longer displayed. But the HttpErrorResponse status always return 0 instead the correct status returned by the server.
I want to know why Chrome and FireFox can show 401 on console while Angular caught 0.
Are these browsers changed 401 to 0 before browser API passed the result to js .
@danwulff
HI... after a loooooong time on it...i found the issue.. its of course in server side. you need to set the CORS middle ware first then the remaining API middlewares
'api' => [
'throttle:60,1',
'bindings',
\Barryvdh\Cors\HandleCors::class,
Currect Code
'api' => [
\Barryvdh\Cors\HandleCors::class,
'throttle:60,1',
'bindings'
For anyone that is using swagger at back-end:
In your default.yaml
file, remove cors
under swagger_controllers
:
swagger_controllers:
#- cors
- swagger_params_parser
- swagger_security
- _swagger_validate
- express_compatibility
- _router
Then in your app.js
file, add cors middleware before any other middlewares :
const cors = require('cors')
app.use(cors());
app.use(bodyParser.json()); // other middlewares
app.use(bodyParser.urlencoded({
extended: true
app.use(httpContext.middleware);
I faced with the same issue - not getting correct HttpError object, Error status was always set to 0. And i think found a reason. So as my Application and API was on different domains - CORS mechanism is applied.
According to CORS for each API request browser sends two requests:
preflight OPTIONS request to understand if API allows Actual/Origin request.
when API allows (OPTIOS request respond with status 204 and correct Access-Control-Allow-Origin headers) - browser send next "Actual/Origin request".
In our Angular Application we are handling Error response for "Actual/Origin request", and if "preflight OPTIONS request" failed - browser doesn't give correct HttpError object for error handler.
So to get correct status of http response - be sure to get success preflight OPTIONS request response.
This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.