在请求的资源node.js上没有Access-Control-Allow-Origin头 [英] No Access-Control-Allow-Origin header is present on the requested resource node.js

查看:678
本文介绍了在请求的资源node.js上没有Access-Control-Allow-Origin头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试从ajax请求与我的node.js服务器进行通信时遇到问题。

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.

当我在浏览器中输入URL时,我已经阅读了很多有关CORS的事情,同样的原因政策,但现在我很迷失。

When I type URL in a browser it works. I have read many things about CORS, same origin policy but now I'm quite lost.

有人可以帮我吗?

感谢。

推荐答案

要解决这个问题,请在server.js中使用以下代码

To solve this problem use the following code in server.js

app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type, Authorization');
    next();
});

这篇关于在请求的资源node.js上没有Access-Control-Allow-Origin头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆