Origin is not allowed by Access-Control-Allow-Origin
📝 Why am I seeing "Origin is not allowed by Access-Control-Allow-Origin" error?
You're here because you encountered the pesky "Origin is not allowed by Access-Control-Allow-Origin" error. Don't worry, you're not alone! This error occurs when a web page tries to make an AJAX request to a different domain, and the server doesn't allow cross-origin resource sharing (CORS).
🔍 What causes this error?
The error typically occurs when you're developing a web application that tries to fetch data from an API or a server located on a different origin. Browsers enforce the same-origin policy, which prevents JavaScript running on one domain from making requests to another domain, for security reasons.
🚀 How can you fix this problem?
Here are a few easy and effective solutions to resolve the "Origin is not allowed by Access-Control-Allow-Origin" error:
Server-side solution
Modify the server's response headers to explicitly allow cross-origin requests. In your case, as you're using a PHP server, you can add the following code to your PHP file:
<?php header("Access-Control-Allow-Origin: *"); ?>
This code sets the
Access-Control-Allow-Origin
header to allow requests from any origin. If you want to restrict access to specific origins, you can replace the*
with the desired origin(s).Using a proxy server
If you don't have control over the server's response headers, or if modifying them is not an option, you can set up a proxy server. A proxy server acts as an intermediary between your web application and the server you're trying to fetch data from. This way, the same-origin policy won't be violated.
You can create a server-side script (e.g., in PHP, Node.js, or Python) that fetches the data from the remote server and relays it to your web application. Your web application can then make requests to this proxy server instead of directly to the remote server. This bypasses the restriction imposed by the same-origin policy.
Here's an example of how you can implement a simple proxy server using Node.js:
const express = require('express'); const request = require('request'); const app = express(); app.use('/', (req, res) => { const url = 'http://nqatalog.negroesquisso.pt/login.php' + req.url; req.pipe(request(url)).pipe(res); }); app.listen(3000, () => { console.log('Proxy server running on port 3000'); });
This code sets up a proxy server that listens on port 3000. When a request is made to this server, it forwards the request to the remote server and relays the response back to the web application.
CORS browser extensions
Another quick solution is to use browser extensions that bypass the same-origin policy and allow cross-origin requests during development. One popular extension is Moesif CORS.
📣 Take action and get rid of that error!
Now that you have a variety of solutions to fix the dreaded "Origin is not allowed by Access-Control-Allow-Origin" error, it's time to take action! Choose the solution that best suits your situation and get back to building awesome web applications.
If you have any questions or need further assistance, feel free to leave a comment below. Let's help each other overcome these challenges!
Happy coding! 👩💻👨💻