-1

I'm developing a website and the forms rely on jQuery $.ajax which will post the data and then send back a result.

But I have a problem.

If I access the site through 'http://site.com/' and click the submit button on a form, it works no problem.

However, if I access the site through 'http://www.site.com/' and click the submit button on a form, it throws an error at me.

Does anyone know why this is?

Thanks.

Mark Eriksson
  • 1,455
  • 10
  • 19

2 Answers2

2

This is called cross-domain restrictions. AJAX will only let you go through to the same domain as the one you are using to load the page, with very very few exceptions. It is possible to tunnel through using JSONP.

This is a security measure. Imagine if a remote attacker was able to AJAX out your info - an AJAX call is extremely easy to put in! Browser manufacturers added this as a security measure to prevent information leakage (which is already far too common - XSS, CSRF et al).

Consider not hardcoding your URLs in your scripts if they have to call locally.

Sébastien Renauld
  • 19,203
  • 2
  • 46
  • 66
2

There is a good chance you can't access the site in any way using www as the site owner did not set the DNS up yet to work with the www prefix.

What have you tried
  • 11,018
  • 4
  • 31
  • 45
  • OP said `"However, if I access the site through 'http://www.site.com/' and click the submit button [...]"` so there's nothing wrong with DNS, the initial page loads fine with `www`. – tcovo Mar 28 '13 at 00:18