Origin is not allowed by Access-Control-Allow-Origin

Cover Image for Origin is not allowed by Access-Control-Allow-Origin
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

📝 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:

  1. 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).

  2. 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.

  3. 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! 👩‍💻👨‍💻


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello