Thursday, July 3, 2008

AJAX-3(AJAX DEVELOPEMENT GOTCHAS)

AJAX DEVELOPEMENT GOTCHAS:-

1-XMLHttpRequest can’t access remote servers:-
For some odd reason, although there are ways in javascript to send and receive data to remote servers, such as through script or iframe elements, XMLHttpRequest cannot have a remote server as a target. This is a basic cross browser security rule that may not be immediately obvious to the newcomer to Ajax development.


2-Multiple Ajax Requests are not fired in order:-
IE, breaking from a long tradition of ignoring independent standards, chooses to follow the HTTP 1.1 RFC 2616 to the letter, which means that IE may only have two XMLHttpRequests open at a time; after which IE will retain an internal queue of requests which will be serviced in no particular order. Even if the requests are fired in order, the nature of the internet dictates that they will not be received in order, so never write code that assumes XMLHttpRequests will be sent in a particular order.
Firefox also has a similar albeit more liberal limitation in the number of simultaneous XMLHttpRequests that may be open; however in Firefox 1.5, the developer may modify the priority of the internal request queue.


3-Asynchronous XMLHttpRequests responses will arrive in no particular order:-
As implied by the word Asynchronous, XMLHttpRequest responses may arrive at any time in an unpredictable order that is ignorant of developer intent, happily executing callbacks in the random order in which they eventually wind up on the client.


4-XMLHttpRequest does not require the use of XML:-
While Ajax is aptly named when it comes to asynchronous execution, it is poorly named when it comes to XML. The XMLHttpRequest object has a useful method called responseText that delivers the straight text of the response, be it XML, JSON or just simple unadorned text. Using JSON or simple text can be much faster, easier, and more concise than XML.


5-Ajax uses UTF-8:-
Normal forms are sent using the encoding of the parent page. Thus a SJIS encoded page will default to sending form content encoded in SJIS. Ajax submitted forms on the other hand will be sent as UTF-8. If for some strange reason, UTF-8 is not the character set of choice for the server, this will require a solution such as the server recognizing and translating UTF-8 responses to a desired character encoding.


6-Ajax requests are url encoded:-
A bug relating to this can currently be seen on Digg.com. Submit a comment to digg, and then edit the comment to include a unicode character. The updated comment, sent via Ajax will be added to the database as the url encoded string: resulting in an odd looking url encoded comment.


7-XMLHttpRequest cannot transmit files:-
In a javascript heavy application, a developer might need to include the ability to send files without completely refreshing the page. XMLHttpRequest however does not include this ability. Instead, a hidden iframe can be used to send the file, and Javascript can simply inspect the iframe. Ajax techniques can further complement this, for example the open source project Uber-Uploader can show a progress bar while uploading a file.


8-Ajax data transfer is text based :-
If you are using Javascript to create data outside of text encoding, and you try to send it through over an XMLHttpRequest, you may notice corruption in your transmission as some characters are not transmitted properly. To solve this, use the native JavaScript escape() before using the encodeURIComponent function, then use the unescape function when the data comes back later.

Firefox quirks or bugs:-

Synchronous XMLHttpRequest luck up Firefox :-
The XMLHttpRequest method provides a third argument, which defines whether the Ajax requests are submitted synchronously or asynchronously. (xmlhttprequest.open(‘GET’, ‘http://www.mozilla.org/’, false);) On Firefox, setting this value to false will submit the xmlhttprequest synchronously and lock up the entire browser for the duration of the request.


IE quirks or bugs:-
1-XMLHttpRequest Objects are not reused in IE:-
Unless the abort method is used, XMLHttpRequest objects in IE won’t function after the first use, unlike in other browsers such as Firefox. One solution to this is to create a new XMLHttpRequest object for each connection. Some fear this causes memory leaks, but there seems to be no conclusive proof of this as long as the known closures memory leak issue is avoided (detailed later).
Another solution to this issue is to reverse the order of the callback handler code in Javascript. More info on this solution can be found in the article Reusing XMLHttpRequest object in IE


2-IE doesn’t use cached images when Javascript inserts HTML with images:-
This is a general JavaScript behavior, but with Ajax heavy applications that insert a lot of HTML snippets, this can lead to a lot of unnecessary image redownloading. The way to solve this is to send all images with proper Cache-Control, ETag, and Last-Modified headers. IE will request images with a ‘If-Modified-Since’ header and the server should send back a 304 header which will tell IE not to redownload the image, and use its cached image.


3-Closures with circular references in IE cause memory leaks:-
It’s common in Ajax to use closures, however in IE if a closure has a circular reference, it will cause IE to start leaking memory. In Javascript it’s easy to create closures with circular references, so care should be taken to avoid memory leaks.


4-Avoiding aggressive caching in IE:-
Even with proper ‘no-cache’ headers, IE caches ‘GET’ requests – this can be solved by changing the get request query string with every request.
An alternative solution for this is to use “If-Modified-Since” request header to point to some past date using xhr.setRequestHeader(“If-Modified-Since”,”some past date”); If you can’t modify the query string, this is a workaround that will stop IE from caching too aggressively.


5-IE corrupts gzipped JavaScript files:-
Although this is a more general issue with IE’s handling of gzipped content, in Ajax applications where there is a lot of JavaScript, developers may be tempted to gzip their JavaScript to allow for faster page loading and lowered bandwidth consumption. However some versions of IE have a known bug where downloads are cut off mid-file, corrupting a downloading gzipped JavaScript file.


6-IE doesn’t cache gzipped JavaScript files:-
Adding to the poor handling of gzipped content in some versions of IE is IE’s lack of support for simultaneous gzip and ETag cache headers. Again, this is wider scope than simply JavaScript, but it means that even if IE didn’t corrupt the gzipped JavaScript, it wouldn’t cache it anyways.

No comments: