The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Elaine (6) [Avatar] Offline
#1
The version of ContentLoader downloadable from this website works OK but generates the following error on the FireFox JavaScript console when run from the UNIX server at my college:

NS_ERROR_NOT_AVAILABLE .. XMLHTTPRequest.status ...

from the line in ContentLoader, in function onReadyState(), when you access this.req.status. Apparently my server complains when you try to access this.req.status before this.req.readyState == READY_STATE_COMPLETE.

I see that the version you have on page 75 of the text doesn't try to do this, so it probably does not generate this error. I have modified the downloadable code slightly, to get:

// Called by the server every time this.req.readyState changes.
// Does nothing until the readyState changes to READY_STATE_COMPLETE,
// at which time it calls the onload function or the onerror function
net.ContentLoader.onReadyState=function(){
if (this.req.readyState==net.READY_STATE_COMPLETE){
if (this.req.status==200 || this.req.status==0){
this.onload.call(this);
// allows the passing of this context to the callback function
// so the callback function has access to req.
}else{
this.onerror.call(this);
}
}
}

Could you please modify your downloadable code so that my students can go to your website to find a version that doesn't generate JavaScript errors on the FireFox JavaScript console?

Thanks for a great book!

-Elaine Haight
Foothill College
Los Altos Hills, CA

ps: after previewing, I see that my indentation is thrown out, sorry...
Elaine (6) [Avatar] Offline
#2
Re: ContentLoader generating JavaScript errors
Could you tell me why ContentLoader doesn't work on Safari? I find that Safari works when running on a Unix server, but not when running on a local Macintosh.
davecrane (149) [Avatar] Offline
#3
Re: ContentLoader generating JavaScript errors
Hi Elaine,

Which version of Safari are you using? I've got a Mac mini running OSX 10.4, and the ContentLoader's been happy on that.

I don't fully understand what you mean that it works on a UNIX server but not a local Mac. The JavaScript code executes in the browser, the server just delivers dumb content, so which server it comes from shouldn't make a difference.

One exception to that - if you're reading a response as XML, it'll need to have the mime type set correctly - either text/xml or application/xml will do. If the mime type, or lack thereof, is the problem, you'll see the XML text in responseText, but responseXML will be null.

Every web server I've come across sets the mime type OK for static docs with a .xml filename, but the local filesystem doesn't - maybe that's what you mean by running off a local mac?.

HTH

Dave
Elaine (6) [Avatar] Offline
#4
Re: ContentLoader generating JavaScript errors
I am using Safari 2.0.4, and Mac OS 10.0.7

I am using ContentLoader to load an xml file. FireFox has no trouble loading and reading the file from both the local drive and the remote server. Safari is fine when the xml file is on the remote server, but when reading the xml file from the local drive, I get
"error fetching data! readyState: 4; status: undefined; headers: undefined"

It is true that I did not set the mime type in the xml file. How do I do that? Do I have to define a schema in order to set the mime type in my xml document?

Thanks,
Elaine
davecrane (149) [Avatar] Offline
#5
Re: ContentLoader generating JavaScript errors
Hi Elaine,

> I am using Safari 2.0.4, and Mac OS 10.0.7

My mac is reporting OSX 10.4.8 and Safari 2.0.4, so if the version of Safari matches, I guess we should expect the same behaviour on the whole.

>
> I am using ContentLoader to load an xml file. FireFox
> has no trouble loading and reading the file from both
> the local drive and the remote server.

I'm pleasantly surprised that this worked! (see my explanation below)

> Safari is fine
> when the xml file is on the remote server, but when
> reading the xml file from the local drive, I get
> "error fetching data! readyState: 4; status:
> undefined; headers: undefined"
>
> It is true that I did not set the mime type in the
> xml file. How do I do that?

You can't set the mime type inside the XML file AFAIK - on a web server the mime type of static content is determined by the server itself, usually based on a mapping to file extensions. When you're using the file:// protocol instead (i.e. reading off the file system) then there is no equivalent matching process, so the mime type is not set. (The MIME type is read by the browser in the HTTP response's 'Content-type' header. When a file is returned from a request starting with file://, there are no headers as such, just a body.) Without this header, XMLHttpRequest won't even try to parse the response as XML, and so responseXML doesn't get set.

> Do I have to define a
> schema in order to set the mime type in my xml
> document?
>
No, definitely not. Mime types are a generic feature of HTTP, nothing to do with XML really. We need to provide a mime type for the XML files in order to tell the XMLHttpRequest that it is dealing with XML, but that's it.

So, the hard and fast rule is, if you want to read responseXML, you need to deliver the content over HTTP, i.e. from a web server. There are plenty of small web servers that your students ought to be able to run from their desktops - I use Apache Tomcat mostly for this purpose, and just stick content under webapps/ROOT, but Apache HTTPD is also easy to install these days on most Oses. In fact, come to think of it, Apache is already installed on Mac's (not sure if this was the case in 10.0 days...)

HTH

Dave
Elaine (6) [Avatar] Offline
#6
Re: ContentLoader generating JavaScript errors
Thanks for the response. I got my OS version wrong, it is OS 10.4.7
TTop (2) [Avatar] Offline
#7
Re: ContentLoader generating JavaScript errors
Thanks for posting this discussion -- I was having the same issue under Safari.