Search code examples
javascriptxmlhttprequestserverxmlhttp

Amazon AWS Webserver Cross Origin Requests


I am currently trying to put up a website on AWS EC2 using node.js and mongoDB. So far, I can start the node server and the mongod service without a problem. The network ACLs are also configured to allow access from my PC, and I am having no trouble getting to it using chrome or firefox. However some of my get requests are not answered and I get this error message on my chrome console:

Failed to load ec2-54-173-21-43.compute-1.amazonaws.com:3001: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

I looked on SO and found that someone mentioned configuring the AWS S3 buckets to allow cross origin resource sharing. I followed this link: https://docs.aws.amazon.com/AmazonS3/latest/user-guide/add-cors-configuration.html

So far, using the link as a guide and using the XML code in this guide https://docs.aws.amazon.com/AmazonS3/latest/dev/cors.html

has not been fruitful, I still get that error. Can someone point me in the right direction? The example XML code I used to for the XML document is here:

<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>

Solution

  • Failed to load ec2-54-173-21-43.compute-1.amazonaws.com:3001: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

    This means that you aren't making an HTTP request.

    It is reasonable to infer that when you wrote the URL in your client-side JavaScript, you omitted the scheme from the front of it (https:// for example).

    Consequently, you are trying to make a request to your local file system (since that is where you are loading the HTML document hosting the JavaScript from).

    The CORS configuration of your AWS service isn't relevant to this particular problem because your code isn't trying to contact that service.