BuzzFeed

The XMLHTTPRequest object in javascript with safari does, on occasion, set the status for the return request as 'undefined'. It seems this is a problem with safari 1.3, and I do believe in 2.0 as well, when trying to retrieve pages from the browser cache using XMLHTTPRequest or AJAX as the kids are calling it these days.

I have been loading static XHTML template files for dynamic rendering as a way to make simple reusable dynamic UI components, and to avoid having to generate HTML within javascript. These templates are static XHTML, and they do indeed become cached. When an attempt to retrieve these cached pages using XMLHTTPRequest within safari 1.3 the status is defined as, well, undefined.

So my code was dumping errors left and louie:

if (req.status != 200) {
   alert("http connect error\nstatus: " + req.status);

and the alert looked kinda like:

There was a problem retrieving data from the server
status: undefined

It took me forever to figure it out. Finally, I found a post from an apple mailing list archive that solved the problem for me.

"Basically you want to add something like the following before the call to send."

xmlReq.setRequestHeader('If-Modified-Since', 
         'Wed, 15 Nov 1995 00:00:00 GMT');

lists.apple.com/archives/dashboard-dev

This tells the client (safari) to retrieve any document who's modified date is later the Nov 15, 1995 from the server, none of this new fangled cached document stuff...now, if your fetching an XML document from 1994, well you're out of luck my friend.

update: I should of mentioned that this does in fact stink, as the client is forced to retrieve the document over the network when it has a version of the document cached locally.

tags: ajax,  software,  web
Safari XMLHTTPRequest undefined status bug June 3, 2005
comments (2)
Comments

This can also happen if you return an empty string as your responseText. I had same problem and returned a space as responseText and all was OK.

posted May 31, 2006 8:39 PM by scott schmitz

Have you guys been able to capture redirects in Safari 1.3.x ? It seems if I do an XHR to a URL that gets redirected, Safari just bombs out completely! Now if the XHR is synchronous, it works, and is no problem detecting the redirect via the status property. But if it's asynch, it bombs out and I'm not sure at which point I can capture the status...
Thanks

posted July 21, 2006 6:00 AM by Adonis

Comments are temporarily closed...