Access-Control-Allow-Origin: Dealing with CORS Errors in Angular

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:

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!

时间: 2025-01-14 01:57:44

Access-Control-Allow-Origin: Dealing with CORS Errors in Angular的相关文章

Access control allow origin 简单请求和复杂请求

错误信息: XMLHttpRequest cannot load http://web.image.myqcloud.com/photos/v2/10008653/bhpocket/0/?sign=4FcLKd5B8-p4SkFVUEJtZ1omZT0xNDQ0NzExMDE5JnQ9MTQ0NDcwNzQxOSZyPTEzMDMyMDgzOTAmdT0wJmY9.No 'Access-Control-Allow-Origin' header is present on the requeste

关于Access Control Allow Origin错误的问题

前天第一次使用jquery向服务器发送Get请求.结果吸拜了...查看console提示说"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.",嗯好吧,google了一下才知道是跨域问题. 跨域访问:AJAX 跨域访问是用户访问A网站时所产生的对B网站的跨域访问请求均提交到A网站的指定页面

Server-Side Access Control

Firefox 3.5 implements the W3C Access Control specification.  As a result, Firefox 3.5 sends specific HTTP headers for cross-site requests initiated from withinXMLHttpRequest (which in Firefox 3.5 and beyond can be used to invoke different domains) a

Oracle Applications Multiple Organizations Access Control for Custom Code

文档 ID 420787.1 White Paper Oracle Applications Multiple Organizations Access Control for Custom Code Checked for relevance on 12-JAN-2011 See Change Record This document discusses how to update the customization code that is affected by the access co

Browser security standards via access control

A computing system is operable to contain a security module within an operating system. This security module may then act to monitor?access?requests by a web browser and apply?mandatory?access?control?security policies to such requests. It will be ap

Access control differentiation in trusted computer system

A trusted computer system that offers Linux? compatibility and supports contemporary hardware speeds. It is designed to require no porting of common applications which run on Linux, to be easy to develop for, and to allow the use of a wide variety of

CISSP AIO 3th: Access Control

This chapter presents the following: • Identification methods and technologies • Authentication methods, models, and technologies • Discretionary, mandatory, and nondiscretionary models • Accountability, monitoring, and auditing practices • Emanation

A GUIDE TO UNDERSTANDINGDISCRETIONARY ACCESS CONTROL INTRUSTED SYSTEMS

1. INTRODUCTION ? The main goal of the National Computer Security Center is to encourage the widespread availability of trusted computer systems. In support of that goal a metric was created, the Department of Defense Trusted Computer System Evaluati

Enhancing network controls in mandatory access control computing environments

A?Mandatory?Access?Control?(MAC) aware firewall includes an extended rule set for MAC attributes, such as a security label or path. Application labels may be used to identify processes and perform firewall rule-checking. The firewall rule set may inc