webpack-dev-server icon indicating copy to clipboard operation
webpack-dev-server copied to clipboard

No 'Access-Control-Allow-Origin' header is present on the requested resource.

Open ghost opened this issue 8 years ago • 5 comments

  • Operating System: Windows 10
  • Node Version: 8.11.1
  • NPM Version: 5.6.0
  • webpack Version: 4.8.3
  • webpack-dev-server Version: 3.1.4
  • [x] This is a bug
  • [ ] This is a modification request

Code

  // webpack.config.js
var path = require('path');

module.exports = {
  mode: 'development',
  entry: './src/index.js',
  output: {
    path: path.resolve(__dirname, 'dist'),
    filename: 'bundle.js'
  },

  node: {
    console: false,
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
  },

  devServer: {
    headers: {
      'Access-Control-Allow-Origin': '*'
	}
  },

};
// index.js
var DigitalOcean = require('do-wrapper'),
    api = new DigitalOcean('API KEY', 1);

api.account().then((data) => {
  console.log(data.body);
});

Expected Behavior

Should console.log something like this:

{
  "account": {
    "droplet_limit": 5,
    "email": "[email protected]",
    "uuid": "57e96b88ed1511e490ec1681e6b88ec1",
    "email_verified": true
  }
}

Actual Behavior

These errors in the console

2account:1 GET https://api.digitalocean.com/v2/account 401 ()
localhost/:1 Failed to load https://api.digitalocean.com/v2/account: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access. The response had HTTP status code 401. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
localhost/:1 Uncaught (in promise) TypeError: Failed to fetch
Promise.then (async)
(anonymous) @ index.js:4
./src/index.js @ bundle.js:3705
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:69
(anonymous) @ bundle.js:72

For Bugs; How can we reproduce the behavior?

Clone https://github.com/jrdnbrn/webpack-demo repo and run npm install in the directory, then run node_modules\.bin\webpack-dev-server.cmd

ghost avatar May 12 '18 23:05 ghost

Seeing this as well

natew avatar Jun 03 '18 06:06 natew

Same you. Wait for someone.

nontachaiwebdev avatar Jul 18 '18 16:07 nontachaiwebdev

any update on this?

Sokrates80 avatar Aug 11 '18 20:08 Sokrates80

Are you trying to explore your server app from the browser? This is a browser security feature. Probably caused because your UI is served in a different location than a server you're trying to access.

I presume your index.js file is part of a different server? It should set up CORS on its config.

Let's say that you're using express:

https://github.com/expressjs/cors#usage

As a temporary workaround during development and while someone on a "team" fixes this, you may:

  1. Start your browser on "security disabled mode". In google chrome:
  • <chrome-cmd-app> --args --user-data-dir=/tmp/temp_chrome_user_data_dir --disable-web-security or
  1. Set up a reverse proxy, forward both client and server requests to the same ip:port. Then whichever client (browser) you use won't complain about CORS. This is a bit less temporary a solution than [1], maybe, but you might get other follow-up clarifications on best practices around this from experts (I'm not one).

Hopefully some of this helps.

ccjoel avatar Sep 24 '18 14:09 ccjoel

Thanks for your help @teh0xqb. I'm closing this since this doesn't seem to have anything to do with WDS.

SpaceK33z avatar Sep 24 '18 19:09 SpaceK33z