添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement . We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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.

this.http .get<ILead>(this.leadApiUrl) .subscribe( data => { console.log('data' + data); this.company = data; }, err => { console.log(err); })

@sharpcoder28 Chrome (and possibly other browsers) return a status of 0 when incorrect CORS headers are used. This is likely some problem with the headers you are sending/receiving from your server and not Angular itself (I could be wrong but I myself have struggled with CORS and it has never been Angular's fault. Although I have had to set specific headers inside angular).

You can find more info about CORS here: https://cloud.google.com/storage/docs/cross-origin

mtpultz, d0x, BrugesDevs, almeidaalex, metju312, trotyl, and rahulz-wasnik reacted with thumbs up emoji zhuhang-jasper, tburnett80, and Psiloscop reacted with thumbs down emoji All reactions

Hi everyone,

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

Wrong Code

'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.