D G
D G

Reputation: 80

ajax fails due to cross domain issue based on existence of www or not

The problem is very simple, but solving it is very confusing for me, please help :)

Problem:

when going to my project site: duskoflife.com and logging in, everything works fine.

However! When going to www.duskoflife.com and logging in, this error appears and logging in fails: XMLHttpRequest cannot load http://duskoflife.com/login.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://www.duskoflife.com' is therefore not allowed access.

Basically the one with www is as if it is a different site, and I sort of understand why it is happening, but how to fix it?

My thoughts: maybe in the beginning detect users who visit www.duskoflife.com and redirect them to duskoflife.com? Why is there even a difference? Is this a proper approach?

Is there somewhere in code I can make the url work for both?

I am sorry if I'm a newb at this, I would love your help and solutions

Upvotes: 0

Views: 45

Answers (1)

adnan kamili
adnan kamili

Reputation: 9455

What you are looking for is CORS. You need to enable it in your server. Now to enable cors you need to modify the http response header sent by your server. It is usually a two or three lines of extra code depending upon your server side language php, .NET etc. You can easily find a code snippet from the following link:

http://enable-cors.org/server.html

Alternatively, you can modify your .htaccess file to redirect www yourdomain.com to yourdomain.com

Upvotes: 1

Related Questions