views:

35

answers:

2

Could someone enlighten me as to why including www in a ajax request causes it to fail.

i.e. This works:

$('#mydiv').load('http://mydomain.com/getitems');

But this doesn't (returns nothing)

$('#mydiv').load('http://www.mydomain.com/getitems');

Note that www.mydomain.com/getitems is a valid domain, in the sense that if I point my web browser to it I am able to load the page.

+3  A: 

Are you calling the script from mydomain.com or from www.mydomain.com or from otherdomain.com ?

May be you are trying to do some kind of cross site scripting with out knowing it .

Sairam Kunala
Yes you were right, when the script is called from using a url including www and the ajax request does not contain the www, it fails and viceversa. I guess the solution is to use relative urls as I can't guarantee how someone will access the page. Is this the only solution?
zenna
The best way would be to either use www.domain.com or domain.com . Either one. For Example: www.friendfeed.com will redirect you to friendfeed.com in order for users remembering form data, history, authentication, cookie info etc. So, its advised you stick with one of the two. If you still want to use both the domains, you could get the present url using javascript :window.location function and parse it.
Sairam Kunala
+1  A: 

AJAX considers www.mydomain.com and mydomain.com to be different domains. AJAX has cross domain restrictions for security reasons. If you absolutely need to do this there are ways to do cross domain AJAX querying with jQuery.

mVChr