0

There is a MySQL database and a Tomcat server that I am attempting to make a GET request to, using ReactJS + Node.js Express:

My GET request is:

  fetchAPI() {
    var data = {
      'Content-Type': 'application/json',
      'start': 'TEST1234567890'
    }

    var myInit = {
      method: 'GET',
      mode: 'cors',
      header: data,
    };

    return function(dispatch) {
      dispatch({
        type: 'FETCH_API'
      });

      fetch('http://www.localhost:8080/form/upload/post/', myInit)
        .then(result=>result.json())
        .then(info=>{
          console.log(info);
        });

    }
  }

With my Express set up as:

var express = require('express');
var path = require('path');
var config = require('../webpack.config.js');
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');

var app = express();

var compiler = webpack(config);

app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: config.output.publicPath}));
app.use(webpackHotMiddleware(compiler));

app.use(express.static('./dist'));

app.use('/', function (req, res) {
  res.sendFile(path.resolve('client/index.html'));
});

var port = 3005;

app.listen(port, function(error) {
  if (error) throw error;
  console.log("Express server listening on port", port);
});

And I am getting the error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3005' is therefore not allowed access. The response had HTTP status code 415. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Everything works and runs fine, but once the GET is called, I get the error above.

What am I missing? Any guidance or insight would be greatly appreciated. Thank you in advance!

Jo Ko
  • 7,225
  • 15
  • 62
  • 120
  • http://enable-cors.org/server_expressjs.html – lux Aug 08 '16 at 19:24
  • @lux Would I write it with the sendFile? I attempted with `res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next();` ` But it said `Cannot GET/` on the page.` – Jo Ko Aug 08 '16 at 20:13
  • @lux just checking to see if you've seen my last comment. Please let me know. – Jo Ko Aug 09 '16 at 20:04
  • Sorry, must have missed it. My understanding is you're just setting properties on the response object, which will get picked up once you execute `res.sendFile`. This might have more info: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-node-js That link I provided seems to have died – lux Aug 09 '16 at 20:06
  • @lux Do you mind showing a sample? – Jo Ko Aug 10 '16 at 06:52

0 Answers0