https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/

Getting this error in your Angular app?

No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

You’ve run afoul of the Same Origin Policy – it says that every AJAX request must match the exact host, protocol, and port of your site. Things that might cause this:

  • Hitting a server from a locally-served file (a request from file:///YourApp/index.html to http://api.awesome.com)
  • Hitting an external API (a request from http://yourapp.com to http://api.awesome.com).
  • Hitting an internal API (a request from http://yourapp.com to http://api.yourapp.com).
  • Hitting a different port on the same host (webapp is on http://localhost:3000, API is http://localhost:4000)
  • Requesting over http from https or vice-versa (requesting https://yourapp.com from http://yourapp.com)

To be clear, this is not an Angular error. It afflicts all web apps equally, and most of the fixes we’ll look at below are actually modifying the server or the browser.

How to fix it

Here are a few ways to solve this problem:

Best: CORS header (requires server changes)

CORS (Cross-Origin Resource Sharing) is a way for the server to say “I will accept your request, even though you came from a different origin.” This requires cooperation from the server – so if you can’t modify the server (e.g. if you’re using an external API), this approach won’t work.

Modify the server to add the header Access-Control-Allow-Origin: * to enable cross-origin requests from anywhere (or specify a domain instead of *). This should solve your problem.

2nd choice: Proxy Server

If you can’t modify the server, you can run your own proxy. And this proxy can return the Access-Control-Allow-Origin header if it’s not at the Same Origin as your page.

Instead of sending API requests to some remote server, you’ll make requests to your proxy, which will forward them to the remote server. Here are a few proxy options.

3rd choice: JSONP (requires server support)

If CORS and the proxy server don’t work for you, JSONP may help. You essentially make a GET request with a callback parameter:

(get) http://api.example.com/endpoint?callback=foo

The server will wrap the JSON reply in a function call to your callback, where you can handle it:

foo({"your": "json", here: true})

There are some downsides, notably that JSONP only supports GET requests and that you still need a cooperative server.

Dev-Only: Disable Same Origin

If this is only for development or learning purposes, the easiest thing to do is to disable the Same Origin Policy in your browser. Be aware that if you do this, you’re opening your browser up to security risks. Follow these instructions:

  • Chrome
  • Firefox

This is more of a last resort. Modifying the server to support CORS or running a proxy are the best approaches.

Armed and Dangerous

You’re all set now to tackle any Access-Control-Allow-Origin errors that come your way!

转载于:https://www.cnblogs.com/kungfupanda/p/5536714.html

Access-Control-Allow-Origin: Dealing with CORS Errors in Angular相关推荐

  1. php access control allow origin,js请求跨域问题--Access-Control-Allow-Origin

    在前台调试的时候出现XMLHttpRequest cannot load http://www.xx.com/Action/Index.php?Action=11. No 'Access-Contro ...

  2. has been blocked by CORS policy: Response to preflight request doesn‘t pass access control check

    has been blocked by CORS policy: Response to preflight request doesn't pass access control check 这个错 ...

  3. 关于跨域 Response to preflight request doesn‘t pass access control check

    做项目的时候由于访问了不同的服务器,然后导致了跨域问题,报错情况为: has been blocked by CORS policy: Response to preflight request do ...

  4. Response to preflight request doesn‘t pass access control check: The value of the ‘Access-Control-Al

    错误:Response to preflight request doesn't pass access control check: The value of the 'Access-Control ...

  5. GO + React + Axios Response to preflight request doesn't pass access control check: It does not hav

    使用Go + Reat 使用 Axios 请求后端, 出现: Access to XMLHttpRequest at 'http://127.0.0.1:20002/v1/user/login' fr ...

  6. 关于Springboot中跨域问题的解决(Response to preflight request doesn‘t pass access control check)

    Springboot中跨域问题的解决 等不及的小伙伴,直接跳到结论部分即可,谢谢!!! 1. 背景 1.1 使用技术栈 Spring Security Springboot Vue.axios Jwt ...

  7. 跨域Response to preflight request doesn‘t pass access control check: It does not have HTTP ok status.

    最近后端添加自定义请求头拦截器后,前端也增加请求头后,解决过程中先后出现两种跨域错误. 一.请求后台出现如下跨域问题: has been blocked by CORS policy: Respons ...

  8. [认证授权] 6.Permission Based Access Control

    在前面5篇博客中介绍了OAuth2和OIDC(OpenId Connect),其作用是授权和认证.那么当我们得到OAuth2的Access Token或者OIDC的Id Token之后,我们的资源服务 ...

  9. ORA-24247: network access denied by access control list (ACL)

    Using UTL_SMTP to send email from your oracle database has changed in Oracle 11g. Or – to be more pr ...

最新文章

  1. 关于Linux,你该读哪些书
  2. Eclipse CDT中EOF输入的解决方法
  3. python面试常见问题-Python面试常见的8个问题及答案总结
  4. UIActivityIndicatorView
  5. 关于expres模板引擎,Function,with
  6. 【评分】第三次作业-团队展示
  7. 【ClickHouse 技术系列】- 在 ClickHouse 中处理实时更新
  8. 5-2利用MapReduce进行数据排序
  9. 从零手动实现简易Tomcat
  10. 《『若水新闻』客户端开发教程》——05.设计新闻列表UI
  11. vc++/c++ 汉字取拼音首字母
  12. nema0183 java解析_NEMA-0183(GPRMC GPGGA)详细解释
  13. 等级保护三级基本要求
  14. 2021-10-25 UI设计规范及动态海报
  15. 划重点 2022面试必刷461道大厂架构面试真题汇总+面经+简历模板
  16. intersect 相交 范围_关于CAD二次开发中(范围线自相交)相交线的问题
  17. ZZNUOJ_C语言1134:字符串转换(附完整源码)
  18. jdbc mysql 解密_数据库配置文件jdbc.properties 实现用户名密码加密
  19. Small Tip: 怎么去Schedule一个Analysis for Office的workbook
  20. CCS使用和调试经验

热门文章

  1. APUE-第5章 标准IO库
  2. [2021.11.19]UPC-2021级新生个人训练赛第4场-19278 Problem D 关门
  3. 程序员分七个等级,你属于哪一级?
  4. Android 辅助服务实战-游戏点击器
  5. 中国男人配不上中国女人?
  6. 晚上兼职送外卖靠谱吗?
  7. 夏日悦动“正当红”魅焰红版荣耀畅玩手环A2惊艳开售
  8. 恋爱纪念日(日期问题)
  9. 移动魔百盒问题之我见
  10. 图片旋转90度解决办法