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,530 points
2,124 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,820 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 17, 2020 in Java-Script by Niroj
• 82,820 points
595 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,820 points
131 views
0 votes
1 answer

Error:“Access is denied” error on accessing iframe document object

Hello @kartik, Basically, this error occurs when the ...READ MORE

answered Apr 27, 2020 in Java-Script by Niroj
• 82,820 points
1,291 views
0 votes
1 answer

Error: spawn ENOENT on node.js?

Hii, Before anyone spends to much time debugging ...READ MORE

answered May 5, 2020 in Java-Script by Niroj
• 82,820 points
7,668 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,820 points
9,532 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,820 points
1,058 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,820 points
854 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,820 points
10,637 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,820 points
598 views