AJAX - The Complete Reference

(avery) #1

Chapter 3: XMLHttpRequest Object 103


PART I


You might wonder about the value of such a method given that typically you will be
responsible for forming the data packet to be consumed by the client-side JavaScript. Sorry
to say, proper MIME type usage is not something that many server-side developers have
paid enough attention to. The main reason for this is that browsers, particularly Internet
Explorer, are a bit too permissive in what they do with incorrect MIME types, so developers
often are not forced to get the details right. Internet Explorer will often flat out ignore MIME
types, instead peeking inside the response packet to decide what it is and favoring that over
any Content-type header value encountered. As an example, you can serve a file as text/
plain, but if you have some HTML tags in the first few lines of the file, Internet Explorer
will happily render it as HTML, while more conformant browsers will not and display the
file properly as text. You can see this in Figure 3-8.
Setting MIME types incorrectly on a Web server or in programs has led to numerous
“works in browser X but not in browser Y” errors that the author has observed, including
something as common as Flash content being handled differently in various browsers.
Readers are encouraged to get this particular detail right in the server side of their Ajax
application to avoid headaches and the need for methods like overrideMimeType(). An
overrideMimeType() example can be found at http://ajaxref.com/ch3/overridemime.html.

Multipart Responses


Some browsers, like Firefox, support an interesting property called multipart that allows
you to handle responses that come in multiple pieces. Traditionally this format was used in
an ancient Web idea called server push, where data was continuously streamed from the
Web server and the page was updated. In the early days of the Web, this type of feature was
used to display changing images, simple style video, and other forms of ever-changing
data. Today you still see the concept employed in Webcam pages where images refresh
continuously.
Free download pdf