0

Having issues getting data back from a http post request to an API I've been building. Throws the error:

XMLHttpRequest cannot load (URL to API here). No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

Here's the Angular code on the client side:

$http.post('MyAPI'sURLHere', {date: $scope.information.PubDate})
                    .then(function(response){
                        console.log(response);
                    }, function(error){
                        console.log(error);
                    });

And here's the Node server side code for my API:

app.post('/getThing', function(req, res){
        var date = req.body.date;
        console.log(typeof date);
        var query = Overquery
        var query2 = "alter session set nls_date_format = 'MM/dd/yyyy'";
        console.log(query);
        oracleDB.execute(query2, function(err, result){
                if(err){
                        console.log(err.message);
                }
                else{
                        console.log(result);
                }
        });
        oracleDB.execute(query, function(err, result){
                if(err){
                        console.log(err.message);
                }
                else{
                        res.header('Access-Control-Allow-Origin', '*');
                        res.header('Access-Control-Allow-Methods', 'POST');
                        res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
                        console.log(result.rows);
                        res.json(result.rows);
                }
        });
});

First time building an API so any suggestions and help would be greatly appreciated!

MattA
  • 245
  • 1
  • 3
  • 10
  • You're properly setting cors headers for the POST request, however, you're likely not properly responding to the OPTIONS request that comes before the post request in some cases. – Kevin B Aug 26 '15 at 21:56

2 Answers2

1

Run the following in your project from a bash shell:

npm install cors --save

It will install this: https://github.com/expressjs/cors

Then add this to your server when creating the app.

var express = require('express');
var cors = require('cors');
var app = express();
app.use(cors());

Edit: This will enable CORS for every domain wich is not recomended for security reasons. Check here for further CORS configuration: https://github.com/expressjs/cors#configuring-cors

rahuL islam
  • 500
  • 4
  • 6
Alfonso Presa
  • 1,004
  • 7
  • 11
0

In your node application, this sample of code should solve your issues:

// Allowing X-domain request
var allowCrossDomain = function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Cache-Control");

    // intercept OPTIONS method
    if ('OPTIONS' == req.method) {
      res.send(200);
    }
    else {
      next();
    }
};
app.use(allowCrossDomain);
schankam
  • 9,547
  • 2
  • 14
  • 25