如何在 express js 中添加“res.addHeader(“Access-Control-Allow-Origin", “*")"? [英] How to add 'res.addHeader("Access-Control-Allow-Origin", "*")' in express js?

查看:26
本文介绍了如何在 express js 中添加“res.addHeader(“Access-Control-Allow-Origin", “*")"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用 angularjs 和cordova 作为前端,使用express 和node js 作为充当服务器的后端.我的客户端在 http://localhost:9000 上运行但是,我的 express js 在 http://localhost:3000.我需要从 express API 获取数据.访问时,它说'

In my app I use angularjs and cordova for front-end and express and node js for backend which is acting as server. My client side is running on http://localhost:9000 but, my express js is running on http://localhost:3000. I need to get data from express API. While accessing, it says '

 XMLHttpRequest cannot load http://localhost:3000/data. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.

为此,我需要添加 res.addHeader("Access-Control-Allow-Origin", "*").它是从另一个域访问一个域.如何在express js中添加这个标题?请帮帮我..

For this, i need to add res.addHeader("Access-Control-Allow-Origin", "*"). It is to access one domain from other domain. How to add this header in express js? Please, help me out..

推荐答案

我也开始了解这个,在搜索时我发现了与此相关的内容.

I'm also started learning about this and while searching I found related to this.

Access-Control-Allow-Origin 只接受 * 或单个来源.

Access-Control-Allow-Origin only accepts * or a single origin.

如果您想支持多个来源但不是全部,那么您必须:

If you want to support multiple origins but not all of them, then you must:

查看 Origin 请求头检查它是否在您的可接受来源列表中把它放在 Access-Control-Allow-Origin 响应头中

look at the Origin request header check if it is on your list of acceptable origins put it in the Access-Control-Allow-Origin response header

看看这个对你有帮助.

没有Access-Control-Allow-Origin"-节点/Apache 端口问题

这篇关于如何在 express js 中添加“res.addHeader(“Access-Control-Allow-Origin", “*")"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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