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

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

问题描述

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

  XMLHttpRequest无法加载http:// localhost:3000 / data。请求资源上不存在访问控制允许源标头。因此,原http:// localhost:9000不允许访问。 

为此,我需要添加 res.addHeader(Access-Control -Allow-Origin,*)。它是从其他域访问一个域。如何在express js中添加这个头文件?请帮助我..

解决方案

我也开始了解这一点,同时搜索我发现与此有关。 / p>

Access-Control-Allow-Origin仅接受*或单个来源。



如果要支持多个起始但不是全部,那么你必须:



查看原始请求标题
检查是否在可接受的起始列表上
将其放在Access-Control-Allow-Origin响应标头中



看到这可能对您有帮助。



否访问控制允许原产地 - Node / Apache Port Issue


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.

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 only accepts * or a single origin.

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

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

See this if it could be helpful to you.

No 'Access-Control-Allow-Origin' - Node / Apache Port Issue

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

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