在AWS EC2中的何处添加CORS标头 [英] Where to add CORS headers in AWS EC2
问题描述
我正在从浏览器向我的EC2 AWS服务器发出POST请求,但返回以下错误.
I'm making a POST request from a browser to my EC2 AWS server but getting the following error back.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://www.example.com' is therefore not allowed access. The response had HTTP status code 401.
我已经做了一些研究和发布,我需要在我的EC2 AWS服务器上启用CORS-我了解我应该按照以下内容添加内容
I've done some research and release I need to enable CORS on my EC2 AWS server - I understand that I should be adding something along the lines of
<CORSConfiguration>
<CORSRule>
<AllowedOrigin>http://www.example1.com</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>http://www.example2.com</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
</CORSRule>
</CORSConfiguration>
来自AWS文档: AWS EC2 CORS
但是我不确定我实际上还需要在哪里添加它.我的AWS服务器知识有限.
However I'm unsure of where I actually need to add this too. My AWS server knowledge it limited.
这是一个Neo4j db,调用是:
It's a Neo4j db and the call is:
getRequest = function () {
$.ajax({
url: 'http://exampleEC2:port/',
type: 'POST',
dataType: 'JSON',
contentType: 'application/json;charset=UTF-8',
password: 'xxxxxx',
username: 'xxxx',
data: dataObject,
success: function (data) {
console.log("success");
},
error: function(error) {
console.log(error);
}
});
};
推荐答案
我花了数小时与之搏斗.尝试.htaccess.尝试.conf.到处都是防火墙.
I spent hours wrestling with this. Tried .htaccess. Tried .conf. Fooling around with firewalls.
最后,它是一行php:
In the end it was a single line of php:
header('Access-Control-Allow-Origin: *');
这篇关于在AWS EC2中的何处添加CORS标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!