Most of the content has been taken from Mozilla's Developer Network Using XMLHttpRequest and slightly modified with compatibility with Smartface.
Types of requests
A request made via XMLHttpRequest can fetch the data in one of two ways, asynchronously or synchronously. The type of request is dictated by the optional async argument (the third argument) that is set on the XMLHttpRequest open() method. If this argument is true or not specified, the XMLHttpRequest is processed asynchronously, otherwise the process is handled synchronously. In general, you should rarely if ever use synchronous requests.
There are several types of response attributes defined by the W3C specification for XMLHttpRequest. These tell the client making the XMLHttpRequest important information about the status of the response. Some cases where dealing with non-text response types may involve some manipulation and analysis as outlined in the following sections.
Analyzing and manipulating the responseXML property
If you use XMLHttpRequest to get the content of a remote XML document, the responseXML property will be a DOM Object containing a parsed XML document, which can be hard to manipulate and analyze. There are four primary ways of analyzing this XML document:
Using XPath to address (point to) parts of it.
Manually Parsing and serializing XML to strings or objects.
Using XMLSerializer to serialize DOM trees to strings or to files.
RegExp can be used if you always know the content of the XML document beforehand. You might want to remove line breaks, if you use RegExp to scan with regard to linebreaks. However, this method is a "last resort" since if the XML code changes slightly, the method will likely fail.
Analyzing and manipulating a responseText property containing an XML/HTML document
If you use XMLHttpRequest to get the content of a remote HTML webpage, the responseText property is a string containing a "soup" of all the HTML tags, which can be hard to manipulate and analyze. There are three primary ways of analyzing this HTML soup string:
- Use the XMLHttpRequest.responseXML property.
- Inject the content into the body of a document fragment via fragment.body.innerHTML and traverse the DOM of the fragment.
- RegExp can be used if you always know the content of the HTML responseText beforehand. You might want to remove line breaks, if you use RegExp to scan with regard to linebreaks. However, this method is a "last resort" since if the HTML code changes slightly, the method will likely fail.
Analyzing and manipulating a responseText property containing an JSON data
- Use JSON.parse method
- RegExp can be used if you always know the content of the JSON responseText beforehand. You might want to remove line breaks, if you use RegExp to scan with regard to linebreaks. However, this method is a "last resort" since if the JSON datachanges slightly, the method will likely fail.
response property of the XMLHttpRequest is filled based on the "Content-Type" header value of the response. If the Content-Type header value contains one of those values, then the response property is a string; otherwise it is a Blob.
- text/ (starting with some text)
Handling binary data
Although XMLHttpRequest is most commonly used to send and receive textual data, it can be used to send and receive binary content. There are several well tested methods for coercing the response of an XMLHttpRequest into sending binary data. These involve utilizing the .overrideMimeType() method on the XMLHttpRequest object and is a workable solution.
XMLHttpRequest provides the ability to listen to various events that can occur while the request is being processed. This includes periodic progress notifications, error notifications, and so forth.
Events are required to be assigned before calling open() on the request. Otherwise the progress events may not fire.
Sending data and uploading files
Instances of XMLHttpRequest can be used to send data in two ways:
- using nothing but AJAX like requests individually
- using the FormData API
The second way (using the FormData API) is the simplest and the fastest, but has the disadvantage that the data collected can not be stringified.
The first way is instead the most complex but also lends itself to be the most flexible and powerful.
Using nothing but XMLHttpRequest
Submitting forms without the FormData API does not require other APIs for most use cases.
The same with Blob.
With sending Blob, binary data is sent as binary.
Using FormData objects
The FormData constructor lets you compile a set of key/value pairs to send using XMLHttpRequest. Its primarily intended for use in sending form data, but can be used independently from forms in order to transmit keyed data. The transmitted data is in the same format that the form's submit() method would use to send the data if the form's encoding type were set to "multipart/form-data". FormData objects can be utilized in a number of ways with an XMLHttpRequest. For examples and explanations of how one can utilize FormData with XMLHttpRequests see the Using FormData Objects page.
Get last modified date
Do something when last modified date changes
Differences and not implemented parts of XMLHttpRequest
- responseType property is working differently
- open does not implement user and password optional parameters
- not implemented: