The requested resource does not have the 'Access-Control-Allow-Origin' header in nginx

Although I have come across many similar questions, none of them have helped me solve my specific problem.

jQuery.post( 'http://example.com/api_offer/get_time_left', function(data) {

    console.log('get time');

}, 'json');

When I check the Chrome console, I see this error message:

XMLHttpRequest cannot load http://example.com/api_offer/get_time_left. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '[my local domain]' is therefore not allowed access.

In the nginx site configuration, I have the following settings:

location ~ \.php$ {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              include fastcgi_params;
              fastcgi_read_timeout 300;

              add_header Access-Control-Allow-Origin *;

              proxy_set_header Access-Control-Allow-Origin $http_origin;

        }

I am using Vagrant, but the same error occurs when I try to run it on a Unix server, not just on my localhost. I have checked the error logs for nginx and the domain site, but haven't found anything useful.

Why is it not working even though I have set * to allow any domain? What other options do I have to resolve this issue?

Answer №1

My mistake was in editing the incorrect section of the configuration file.

There were actually two separate server {} blocks in the file.

One of them was designated for SSL, while the other did not have SSL enabled.

Unfortunately, I was making changes to the SSL section.

server {
    liten 443;
    ssl on;


    ....

}

However, I was trying to access the non-SSL part of the site and it was not responding.

The main question that arose was - how could I have realized this error sooner? I ended up spending several hours on this issue.

So, for anyone facing a similar problem in the future, be sure to double-check and confirm that you are modifying the correct block or file within the sites-enabled directory

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a difficulty in sending data to the server through ajax with Cordova

I am currently working on a Phonegap Cordova project and I'm trying to send data to the server using AJAX but I'm encountering an error. Here is an example of my code: $(document).ready(function() { $('#frm').submit(function() ...

Guide on how to reference the Html directory using the path module in Node Js

Desperately trying to send an html file as a response using express, but I'm having trouble locating the position or path of the index.html file. This is how my files are structured - MyWebsite Html index.html Css index.css ...

Is there a way to toggle a single Reactstrap Collapse component?

Currently, I am working on a Next.JS application that displays a list of Github users. The goal is to have each user's information box toggle open and close when clicked, using Reactstrap's Collapse component. However, the issue I'm facing i ...

Displaying an image in a Bootstrap 5 tooltip / Issue 304

Working on a project using Bootstrap 5. I've implemented a tooltip that displays text and an image successfully by using data-bs-html="true". Here is how my input, including the tooltip, is structured: <input class="form-control ...

How can I create a customized scrollbar for a div element in an Angular 2.0 CLI project?

I am attempting to create a sleek horizontal scroll bar within one of my div elements, similar to the example shown here: https://i.stack.imgur.com/ziWhi.png My project is based on the angular2 CLI. Progress so far: I came across this package angular2-s ...

Increase the visibility of a div using Jquery and decrease its visibility with the "

Can anyone assist me with implementing a "show more" and "show less" feature for a specific div? I have put together a DEMO on codepen.io In the DEMO, there are 8 red-framed div elements. I am looking to display a "show more" link if the total number of ...

Animate failed due to the appending and adding of class

$('#clickMe').click(function() { $('#ticketsDemosss').append($('<li>').text('my li goes here')).addClass('fadeIn'); }); <link href="http://s.mlcdn.co/animate.css" rel="stylesheet"/> <script ...

Unable to download essential dependencies using NPM

After cloning a repository for a MEAN stack application, the first step was to run npm install. The installation process resulted in: added 1580 packages from 1887 contributors and audited 15249 packages in 281.586s found 18 vulnerabilities (5 low, 12 mod ...

Delivering XML in response to a webmethod call

While working with an ajax PageMethod to call an asp.net webmethod, I encountered an issue when trying to pass a significant amount of XML back to a callback javascript function. Currently, I am converting the XML into a string and passing it in that form ...

Querying a list of objects with nested pointers using the Parse.com Javascript API

How can I efficiently query my Parse.com backend for a list of objects that contain specific pointers within them? For example, if ObjectA contains a list of pointers to ObjectB, how can I query for all ObjectA's that have ObjectB in their list? I a ...

Creating a stationary div element solely with Javascript, excluding the use of CSS, jQuery, and HTML

I've been searching all day for a solution to this issue without any success. I apologize if I overlooked any relevant posts on the topic. Currently, I am working with Qualtrics and attempting to implement a textbox with scrolling instructions that fo ...

Issues with React Router server-side rendering: Error: Required prop 'history' was not specified in 'RoutingContext' prop validation

I am currently working on a fun little project to improve my React/Hapi skills and so far everything has been smooth sailing until I attempted to set up server-side routing. The server is running without any issues and successfully renders "/" with a simpl ...

Unlike other templating engines, EJS does not automatically escape characters

In my current project, I am utilizing a Node JS server to query MongoDB and then display the results in an EJS template using the code snippet: res.render('graphFabric.ejs', {'iBeacons':[(beacon)]});. However, when attempting to re ...

Is the alert failing to appear during the onbeforeunload event across all web browsers?

Check out the following code snippet that is functional. window.onbeforeunload = function() { someAjaxCall(); } This code block, however, does not perform as expected: window.onbeforeunload = function() { someAjaxCall(); alert("Success !!"); ...

I'm attempting to solve the retrieved data from a firebase query, but unfortunately, the data is refusing to resolve

I've been attempting to retrieve data from firebase using Node.js, but I'm having trouble resolving it. Specifically, I need to extract the timestamp from the data stored in firebase. I've tried using promises and forEach loops, but haven&a ...

JavaScript validation for basic "select" isn't functioning as expected

I need assistance with my simple "select" validation using JavaScript. The issue I am facing is that when validating the "select" element, it does not display the select options to users after a validation error occurs. "The select option is not re-enabl ...

Setting null for HttpParams during the call

I am encountering an issue with HttpParams and HttpHeaders after upgrading my project from Angular 7 to Angular 8. The problem arises when I make a call to the API, as the parameters are not being added. Any assistance in resolving this matter would be gre ...

Transforming a CSV file into JSON format using Gatsbyjs

I am currently exploring the possibilities of GatsbyJs and considering the utilization of the gatsby-transformer-csv plugin. You can find the documentation for this plugin here. I have got my hands on two CSV files exported from WordPress that I am eager ...

What is the process for adding images from CSS (backgrounds, etc.) to the build folder with webpack?

Trying out the file loader for processing images and adding them to my build folder. Images within HTML files successfully show up in the build, but the ones from styles do not. I've divided my webpack configuration into two separate files and use t ...

Waiting for text change in Selenium elements: A comprehensive guide

One of my elements changes its status in text form based on a context menu that is associated with it. Here is the particular element: td id="A" name="status"> StatusStart </td> Under certain circumstances, this can transform into ...