Issue
I want to use of http://5.160.2.148:8091/api/trainTicketing/city/findAll rest for get cities in my angular project.
I used version 7.2.15 of angular in my project.
when get this url with httpClient throw following error :
Access to XMLHttpRequest at 'http://5.160.2.148:8091/api/trainTicketing/city/findAll' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
While at work correctly when enter url in browser and postman.
why ?
Solution
Solution 1 - you need to change your backend to accept your incoming requests
Solution 2 - using Angular proxy see here
Please note this is only for
ng serve
, you can't use proxy inng build
Solution 3 - IF your backend accepts requests from a wildcard domanin like *.mydomain.com then you can edit your hosts
file and add 127.0.0.1 local.mydomain.com
in there, then in your browser instead of localhost:4200
enter local.mydomain.com:4200
Note: the reason it's working via postman is postman doesn't send preflight requests while your browser does.
Answered By - Reza
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.