No ‘access-control-allow-origin’ Header Is Present On The Requested Resource—when Trying To Get Data From A REST API

The error message "No ‘Access-Control-Allow-Origin’ header is present on the requested resource" occurs when you try to make an AJAX request from a JavaScript code to a REST API endpoint that is hosted on a different domain. This error is a security feature implemented by web browsers called Same-Origin Policy.

Same-Origin Policy restricts web pages from making requests to a different domain. That means if your JavaScript code is hosted on "" and you are trying to make an AJAX request to "", the browser will block the request unless the server at "" explicitly allows requests from "" (or all domains) by specifying the ‘Access-Control-Allow-Origin’ response header.

To resolve this error, you have a few options:

  1. CORS (Cross-Origin Resource Sharing): If you have control over the REST API server, you can allow access from different domains by adding the ‘Access-Control-Allow-Origin’ header to the server’s response. This header can be set to ‘*’ to allow access from any domain, or you can specify specific domains.

  2. JSONP (JSON with Padding): If the REST API supports JSONP, you can use it as an alternative. JSONP allows making cross-domain requests since it works by dynamically creating a script tag in the HTML file rather than an AJAX request. Make sure the REST API supports JSONP or has a JSONP endpoint.

  3. Proxy Server: You can set up a server-side proxy that acts as an intermediary between your JavaScript code and the REST API. Your JavaScript code would make requests to the proxy server, and then the proxy server would forward the request to the REST API and return the response back to your JavaScript code. This way, the same-origin policy doesn’t apply since your code is making requests to the same domain.

Note that the solution you choose depends on your specific use case and the level of control you have over the REST API server.

About the Author Rex

I'm a passionate tech blogger with an insatiable love for programming! From my early days tinkering with code, I've delved into web dev, mobile apps, and AI. Sharing insights and tutorials with the world is my joy, connecting me to a global community of like-minded tech enthusiasts. Python holds a special place in my heart, but I embrace all challenges. Constantly learning, I attend tech conferences, contribute to open-source projects, and engage in code review sessions. My ultimate goal is to inspire the next generation of developers and contribute positively to the ever-evolving tech landscape. Let's code together!