The CORS policy has blocked the AWS API Gateway request originating from 'http://localhost:8080'

Currently, I am facing an issue with deploying an API for use in Front End. The API functions properly when tested alone, but it returns a CORS error when integrated into a Vue app. The specific error message is as follows:

Access to XMLHttpRequest at 'https://APIDomain/development/pin' from origin 'http://localhost:8080' has been blocked by the CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I have deployed this API using serverless and configured the allowed origins in the serverless.yml file like so:

service: test-lambda-node
provider:
  name: aws
  runtime: nodejs12.x
  stage: development
  region: ap-east-1
  memorySize: 512
  timeout: 15
functions:
  app:
    handler: lambda.handler
    events:
      - http:
          path: /
          method: ANY
          cors:
            origin: 'http://localhost:8080'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: false
      - http:
          path: /{proxy+}
          method: ANY
          cors:
            origin: 'http://localhost:8080'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token
              - X-Amz-User-Agent
            allowCredentials: false

Even after attempting to use a wildcard to allow all types of origins, the issue persists. Manual enabling of CORS in the AWS API Gateway console has also not resolved the problem. Is there a way to configure the API to enable the CORS policy for any origin?

Answer №1

Issue

Your Vue application is not sending CORS headers.

Resolution

To fix this problem, make sure to include the Access-Control-Allow-Origin header in your Vue app when making API requests.

Note

If you are using cookies, remember to also adjust the allowCredentials setting in your serverless.yml and set Access-Control-Allow-Credentials in your Vue app to true.

Additional Resources

For more information on CORS with Serverless, check out:

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

NodeJS and TypeScript are throwing an error with code TS2339, stating that the property 'timeOrigin' is not found on the type 'Performance'

I am currently working on a NodeJS/TypeScript application that utilizes Selenium WebDriver. Here is an excerpt from the code: import { WebDriver } from "selenium-webdriver"; export class MyClass { public async getTimeOrigin(driver: WebDriver ...

When running multiple tests, a stencil component utilizing a store may experience a loss of context

stencil store: 2.0.9 stencil core: 4.0.2 Issue at Hand In my current stencil test project, I have set up a basic store implementation. However, when I attempt to manipulate the store values and reset them in my test, the test loses its context and is una ...

Why isn't the router returning JSON data?

Why am I not receiving a response from this code? const express = require('express') const mongoose = require('mongoose') const authRouter = require('./authRouter') //importing the router const PORT = process.env.PORT || 3000 ...

The constructor error in ng-serve signalizes an issue in

Currently, I am developing an AngularJS application. However, when attempting to start the dev server, I encountered an issue with my ng serve command: https://i.stack.imgur.com/QujSe.png ...

What is the best way to create a new row within a Bootstrap table?

Struggling to format an array inside a table with the `.join()` method. The goal is to have each car on a separate row. Attempts using `.join("\r\n")` and `.join("<br />")` have been unsuccessful. What am I overlooking? ...

Is the Vue router preloading each view before displaying it, or does it load the view server only upon request?

When I refer to preloading, I am questioning whether the browser loads all views at once or only loads the desired views from the server upon request. ...

Guide on deploying a personalized SDK alongside your nodejs application on the Azure platform

I have created an SDK.js to simplify the usage of my API on Azure, which is supported by my nodejs server. What is the most efficient method for deploying this SDK.js to the server so that clients can easily incorporate it into their code using a script t ...

Issue encountered in loading css and js folders during the build of the Angular2 application due to the files not being found

I have developed an Angular 2 application that utilizes Node.js server APIs. After building the app using nd b, the files were generated in the dist folder. Where should I specify the production URL for the build so that all CSS and JS files load properly? ...

Are you encountering difficulties while managing back pressure as anticipated when applying node request or axios in combination with streams for downloading and extracting files?

We have encountered a peculiar problem while trying to download and decompress a large file of approximately 6 GB (which decompresses to around 64 GB) using the HTTP protocol. To achieve this, we are utilizing either Node.js' request library or axios. ...

The function Amplify.configure does not exist

Currently attempting to utilize AWS Amplify with S3 Storage, following the steps outlined in this tutorial for manual setup. I have created a file named amplify-test.js, and here is its content: // import Amplify from 'aws-amplify'; var Amplify ...

Mastering error handling in IcedCoffeeScript: Best practices and pro tips

When working with node.js, it is a common convention to return an error message as the first argument in a callback function. While there are several JavaScript solutions (such as Promise, Step, seq, etc) that address this issue, none of them seem to be co ...

Generating a unique user ID similar to Zerodha's user ID using Node.js/JavaScript

For a project I'm working on, I need to create random and unique User IDs. I came across Zerodha's user IDs which are easy to remember. In Zerodha user IDs: The first two characters are always letters followed by four numbers. I want to generat ...

Is there a way to assign a specific color to individual users in a chat?

I have successfully implemented a chat feature using Pusher, but now I want to customize the appearance by changing the color of the username for the logged-in user when they are active in the conversation. Currently, both my username and another user&apos ...

Node is currently posing a challenge for the installation of packages

I am currently facing an issue while setting up my raspberry pi 3. I am attempting to install and execute a node code, but I encountered a problem during the installation of packages using npm. After trying multiple times with different versions of node ( ...

Obtaining the Server IP Address for Email Communication in Node.js

Currently, I am utilizing nodemailer to send emails in nodejs. The process of embedding hyperlinks into the email body is functioning smoothly. Below is a snippet of my sample email body: `This is a confirmation email.\n Plea ...

Issue with vuejs: npm run dev command not functioning

I'm facing an issue with running VueJS webpack on a server using the command: npm run dev. Instead of it running smoothly, I am getting a long list of errors. You can view the screenshot of the errors by clicking here. ...

What are the best practices for running node in VSCode?

$ node test.js internal/modules/cjs/loader.js:883 throw err; ^ I have exhausted all possible solutions, including checking the PATH route for Node.js, restarting, and using different files. Despite the fact that I am able to retrieve the version when ...

Angular ui router - Transitioning from one state to the same state, when no parameters are provided, results in

Check out the Plunker Demo <script> var myapp = angular.module('myapp', ["ui.router"]) myapp.config(function($stateProvider, $urlRouterProvider) { // If there is no matching URL, go to /dashboard $urlRouterProvider. ...

How can one create a long-lasting variable in Node.js that persists throughout the application's lifespan?

Currently, I am in the process of developing a web service known as 'A' using Node.js. This service will need to communicate with another service called 'B'. In order for A to properly function, it must initially obtain an access token ...

Displaying the format when entering a value with react-number-format

How to Display Format Only After Full Value Inserted in react-number-format I recently implemented the react-number-format package for formatting phone numbers. import { PatternFormat } from 'react-number-format'; <PatternFormat value={v ...