Error No Access-Control-Allow-Origin header is present on the requested resource node js

0 votes

I have a problem when trying to communicate with my node.js server from ajax requests.

I have configured my server like this :

var allowCrossDomain = function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type,      Accept");
  next();
};

app.use(allowCrossDomain);

When doing my requests I have this error :

XMLHttpRequest cannot load http://10.192.122.180:8181/meters. No 'Access-Control-Allow-     Origin' header is present on the requested resource. Origin 'http://localhost:6161' is  therefore not allowed access.

Could someone help me please ?

Thanks!

Jun 8, 2020 in Java-Script by kartik
• 37,520 points
4,655 views

1 answer to this question.

0 votes

Hello @kartik,

My guess is that you're serving the page before your middleware (allowCrossDomain) gets a chance to run.

If you're using Express 3, try the following:

app.use(allowCrossDomain);
app.use(app.router);

If you're using Express 4, make sure your app.use(allowCrossDomain) call is before the rest of your routes (app.get and such).

Hope it helps!

answered Jun 8, 2020 by Niroj
• 82,840 points

Related Questions In Java-Script

0 votes
1 answer

Error:Origin is not allowed by Access-Control-Allow-Origin

Hello @kartik, The easiest way to handle this ...READ MORE

answered Jun 18, 2020 in Java-Script by Niroj
• 82,840 points
8,948 views
0 votes
0 answers

How does Access-Control-Allow-Origin header work?

Until now my understanding of its semantics ...READ MORE

May 11, 2022 in Java-Script by Kichu
• 19,040 points
851 views
0 votes
1 answer

What is the difference between node.js and io.js?

Hello, io.js: Node-forward is basically being merged into io.js forked on ...READ MORE

answered Apr 24, 2020 in Java-Script by Niroj
• 82,840 points
849 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,012 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
2,995 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,918 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,696 views
0 votes
1 answer

Error:Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers

Hello @kartik, Access-Control-Allow-Headers does not allow * as accepted value. Instead of ...READ MORE

answered Jul 6, 2020 in Java-Script by Niroj
• 82,840 points
2,438 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP