Http.jsm

HTTP.jsm

Http.jsm provides httpRequest - a wrapper for XMLHttpRequest that provides convenient and simplified API for dealing with HTTP requests.

httpRequest supports the following parameters:

name meaning
headers an array of headers
postData

this can be:

  • a string: send it as is
  • an array of parameters: encode as form values
  • null/undefined: no POST data.

method

GET, POST or PUT (this is automatically set if postData exists).
onLoad a function handle to call when the load is complete, it takes two parameters: the responseText and the XHR object.
onError a function handle to call when an error occurs, it takes three  parameters: the error, the responseText and the XHR object.
logger an object that implements the debug and log methods (e.g. log.jsm).

Headers or post data are given as an array of arrays, for each inner array the first value is the key and the second is the value. For example: [["key1", "value1"], ["key2", "value2"]].

Submitting post data

httpRequest allows attaching data to the post requests. The data can be specified via postData option. postData can be of 2 different types: a string or an array of parameters. When null/undefined is given, no post data will be attached. If a string is given the data will be appended to the request as is. In case an array of parameters is given, it will be treated as an array of key-value pairs. The elements of the array will be URL-encoded and "application/x-www-form-urlencoded; charset=utf-8" will be enforced as the content type. Http.jsm only forces the content type if the post data is an Array, and it serializes it automatically. If the provided postdata is a string, the content type isn't touched. In this case, the content type may be set through the headers parameter.

Additional XHR configuration

It is possible to modify things manually on the XHR object after calling httpRequest. httpRequest returns an XHR object, which can be used to set additional parameters for the request. For example, XHR might be configured to use any custom MIME-type when processing a response regardless of what the server returns. To achieve this, you can obtain an XHR object by calling httpRequest and then call its overrideMimeType() with a preferred MIME-Type. Another example of how this can be used can be found here.

Document Tags and Contributors

Tags: 
 Contributors to this page: rolfedh, fscholz, ishe
 Last updated by: rolfedh,