user3766930 user3766930 - 4 months ago 5
Ajax Question

No 'Access-Control-Allow-Origin' header when accessing webpage address starting from www. Without www it works - why?

I have a webservice in my node.js web app (placed on amazon ec2) and it takes email address and saves it to mongodb. This webservice is placed on

of my server.
I wrote a small ajax code that sends the data directly to the webservice.
It looks like this:

url: '',
type: "POST",
data: {
email: $('input[name=subscribe_email]').val()
success: function(response)
var output = '<p style="color: white">Thanks, we will be in touch!</p>';

When in browser I enter my page by typing and provide email - everything works fine. But when I add
and enter my webpage as - then after trying to send email I'm getting a well known error:

XMLHttpRequest cannot load
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin '' is therefore not allowed access.

How can I change my ajax code (or any other possible parts of code) so that I can submit email either from


The access control rules consider the URL method ("http" or "https"), the whole domain name, and the port number. The rules don't "think" about the URL; it's a straight textual comparison.

Thus, "" and "" are considered two distinct domains, and so cross-origin rules apply.

You can use the URL "/mail" in your $.ajax() call instead, which will use the same method and domain and port as the containing page.

(Edited - I was confused or undercaffeinated or both. Should be "/mail" with one slash.)