Axios Cross origin http://localhost forbidden

Active3 hr before
Viewed126 times

8 Answers


If you check the response, you'll notice that there is now an Access-Control-Allow-Origin header with the origin http:://localhost:3000! This means that when you run your client-side app on localhost:3000, the app will be able to retrieve resources from your server,,Notice that in this command, you've changed the origin in your request to http://localhost:4000, which was not in your list of allowed origins

Example_snippet/controller/utility/_axios.js/ Access to fetch at 'http://loc. . .
Access to fetch at 'http://localhost:5000/rooms'
from origin 'http://localhost:3000'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
header is present
on the requested resource.If an opaque response serves your needs, set the
request 's mode to '
no - cors ' to fetch the resource with CORS disabled.
load more v

Cross-Origin Resource Sharing (CORS),Client-Side & Server-Side (Java) sample for Cross-Origin Resource Sharing (CORS),Code Samples Showing XMLHttpRequest and Cross-Origin Resource Sharing,Cross-Origin Resource Sharing From a Server-Side Perspective (PHP, etc,)

Example_snippet/controller/utility/_axios.js/ var invocation = new XMLHttpRe. . .
var invocation = new XMLHttpRequest();
var url = 'http://bar.other/resources/public-data/';

function callOtherDomain() {
   if (invocation) {'GET', url, true);
      invocation.onreadystatechange = handler;
load more v

Unfortunately, when I'm using Axis I get following message Error: Cross origin http://localhost forbidden,

Example_snippet/controller/utility/_axios.js/ it('shoudl work via request', . . .
it('shoudl work via request', async () => {

   var options = {
      method: 'POST',
      url: '',
      headers: {
         'Cache-Control': 'no-cache',
         'Content-Type': 'application/json'
      body: {
         html: 'PGgxPkhlbGxvIHt7bmFtZX19PC9oMT48aDI+SGVsbG8gd29ybGQhISE8L2gyPg==',
         variables: 'eyJuYW1lIjoiV29ybGQiLCJwYXRoIjoiL2hvbWUvcGF3ZWwvLm52bS92ZXJzaW9ucy9ub2RlL3Y4LjExLjMvYmluOi91c3IvbG9jYWwvc2JpbjovdXNyL2xvY2FsL2JpbjovdXNyL3NiaW46L3Vzci9iaW46L3NiaW46L2JpbjovdXNyL2dhbWVzOi91c3IvbG9jYWwvZ2FtZXM6L3NuYXAvYmluOi91c3IvbGliL2p2bS9qYXZhLTgtb3JhY2xlL2JpbjovdXNyL2xpYi9qdm0vamF2YS04LW9yYWNsZS9kYi9iaW46L3Vzci9saWIvanZtL2phdmEtOC1vcmFjbGUvanJlL2JpbjovdmFyL3Rhc2sifQ==',
         css: 'aDEge2NvbG9yOiByZWR9'
      json: true

   request(options, function(error, response, body) {
      if (error) throw new Error(error);
load more v

A cross-origin HTTP request is one that is made to: ,A different domain (for example, from example,com to amazondomains

Example_snippet/controller/utility/_axios.js/ exports.handler = async (event. . .
exports.handler = async (event) => {
   const response = {
      statusCode: 200,
      headers: {
         "Access-Control-Allow-Headers": "Content-Type",
         "Access-Control-Allow-Origin": "",
         "Access-Control-Allow-Methods": "OPTIONS,POST,GET"
      body: JSON.stringify('Hello from Lambda!'),
   return response;
load more v

Example_snippet/controller/utility/_forbidden.js/ headers: {"Access-Control-Allo. . .
headers: {
   "Access-Control-Allow-Origin": "*"

Also ensure the CDN responds with the Access-Control-Allow-Origin: * HTTP header:,You can simplify the development/debugging process by ensuring that errors are thrown with a same-origin policy, Below are some common causes of cross-origin errors and ways to address them

Example_snippet/controller/utility/_forbidden.js/ <script crossorigin src="...">. . .
<script crossorigin src="..."></script>

Example_snippet/controller/utility/_forbidden.js/ // add the following to avoid . . .
// add the following to avoid axios cross origin issues when in test
axios.defaults.adapter = require('axios/lib/adapters/http')

I am using Axios to create a HTTP request to a API server in different domain,,The API server allows Cross-Origin requests from http://localhost:3000

Example_snippet/controller/utility/_forbidden.js/ axios.defaults.adapter = requi. . .
axios.defaults.adapter = require('axios/lib/adapters/http')