1

I'm developing a server at Node js and my front-end at Angularjs, but I'm getting an error when I try to make a request from my front-end:

XMLHttpRequest cannot load http://localhost:8085/server/authenticate/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

My code at my server.js

app.use(function(req,res,next){ res.setHeader('Access-Control-Allow-Oringin','*'); res.setHeader('Access-Control-Allow-Methods','GET,POST,OPTIONS'); res.setHeader('Access-Control-Allow-Headers','X-Requested-With,content-type,Authorization,accept'); if (req.method === 'OPTIONS'){ res.statusCode = 200; return res.end(); } else{ return next(); } }); 

Any suggestions?

NOTE: back-end and front-end are not at the same port, I'm trying to make it work as a CROSS-Origin resourse.

3
  • npmjs.com/package/cors Commented Jan 25, 2016 at 16:48
  • 3
    Access-Control-Allow- Oringin is not a CORS header :) The error message is indeed correct that Access-Control-Allow-Origin is not being set on the response. Commented Jan 25, 2016 at 16:49
  • Thanks a lot, I didn't realized about that Commented Jan 25, 2016 at 18:17

2 Answers 2

1

var express = require('express') , cors = require('cors') , app = express(); app.use(cors()); /* Make your app with CORS support: for more information: https://www.npmjs.com/package/cors*/

Sign up to request clarification or add additional context in comments.

Comments

0

There is a little error in your header name Access-Control-Allow-Oringin

Try this

app.use(function(req,res,next){ res.setHeader('Access-Control-Allow-Origin','*'); res.setHeader('Access-Control-Allow-Methods','GET,POST,OPTIONS'); res.setHeader('Access-Control-Allow-Headers','X-Requested-With,content-type,Authorization,accept'); if (req.method === 'OPTIONS'){ res.statusCode = 200; return res.end(); } else{ return next(); } 

});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.