Akxl Labs C# ASP.NET Articles and Tutorials Akxl Labs Web Apps and Tools for Your Website

AJAX and the WWW Problem

Tags tagged as   Code: AJAX, JavaScript, Web
Got an AJAX Script stuck in readyState 2? You've probably got a WWW conflict.

Posted October 07, 2006    Viewed 2482 times    Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

The Problem

I built an AJAX-based script the worked perfectly during testing on a local computer. When uploaded to the server, it no longer worked.

I opened a new browser window, and the site worked fine. I closed and re-opened the window again, and it failed. This repeated with a random set of the windows working.

After adding some debugging statements, I discovered that the readyState property of the XMLHttpRequest object would get through values 1 (connection open) and 2 (sending request), but would never reach 3 (receiving). For some reason, the XMLHttpRequest object would never receive any response in the affected windows, and became stuck at readyState 2.

After some further debugging statements were added to the ASPX file that was being called by the AJAX script, I was able to determine that a response was sent.

Articles and downloads sponsored by:
Thanks! Amazon commissions help me pay for textbooks.

The Solution

AJAX, when running from a remote page (but not when on a local page), only allows responses from the same domain as the remote page. This security in implemented in the receiving stage, since it is possible to redirect a response server-side after sending the request.

Of course, the page I was viewing and the script being called by the AJAX code were on the same server, and under the same domain. After a very frustrating 15 minutes, I realized that I sometimes type the page's URL with the www prefix, and sometimes don't.

AJAX considers www.akxl.net and akxl.net to be different domains, so a page on www.akxl.net calling back to akxl.net will be blocked by a security restriction, even though it will always work in local testing. As a result, I wrote the below function, which I always call on postback URL's before opening an XMLHttpRequest connection to them.

Source Code

1 function CorrectWWW(theUrl) 2 { 3 // The current page has a www address 4 // The AJAX requested page has an absolute, not relative, address 5 // The AJAX requested page does not have a www address 6 if (window.location.href.indexOf("www.") != -1 && 7 theUrl.indexOf("http://") != -1 && 8 theUrl.indexOf("www.") == -1) 9 { 10 theUrl = theUrl.replace("http://", "http://www."); 11 } 12 13 // The current page does not have a www address 14 // The AJAX requested page has an absolute, not relative, address 15 // The AJAX requested page has a www address 16 else if (window.location.href.indexOf("www.") == -1 && 17 theUrl.indexOf("http://") != -1 && 18 theUrl.indexOf("www.") != -1) 19 { 20 theUrl = theUrl.replace("http://www.", "http://"); 21 } 22 23 return theUrl; 24 }

Comments & Feedback


Anonymous says:
Thursday, October 18, 2007 @ 7:43 AM
thx
Leave this field blank:
Comment on this Entry
This work is licensed under a Creative Commons Attribution 3.0 United States License.
Please link to this article in your source code comments if you use this content.

Labs

Blog

The blog has moved.
Non-technical articles are now on a seperate site.
Contact me for the new address.

Apps

Real-Time Coffee Counter
add it to your website!
Golden Ratio Visualizer
a tool for design

Coffee Counter

Current Count:
Akxl Coffee Meter
Current Coffee:
 Peet's Malawi Songwe River

The Real-Time Coffee Meter is a free Website App from Akxl Labs. Text-only and badge versions available.