• xhr.post() - make an HTTP POST request
Each of these methods takes two arguments:
• url – The target URL of the request
• options – A JavaScript object which sets options on the request
Some of the more prevalent options available include:
• query – An object where the property names will be query parameter names and the
property values will be the corresponding query parameter values.
• handleAs – How the return data should be handled. Values include:
◦ "json" – Handle the response as a JSON encoded object.
◦ "javascript"
◦ "xml"
• headers – An object where the property names will be added as HTTP headers and the
property values as the corresponding value of the property header.
• data – For PUT and POST, this is the payload of the data to be transmitted.
Since the result from calling one of these functions is a Dojo Deferred, we can use the "then()"
method to handle the response. The general form is:
then(function(data) {}, function(data) {});
The first function is called when the REST request returns good data and the second function is
called if an error is detected.
Here is an example call:
xhr.get("/rest/bpm/wle/v1/task/" + taskId + "/clientSettings/IBM_WLE_Coach", {
"handleAs": "json" }).then(function(data) {…}, function(data) {…});
If we wish to add basic authentication headers to the HTTP request, we can do that too. For
example, the following header property will achieve this:
Authorization: Basic QwxhZGRpbjpvcGVuIHNlc2FtZQ==
Where the Base64 encoded text is "username:password". We can generate a base64 encoded
value using:
var bytes = [];
var str = userid + ":" + password;
for (var i = 0; i < str.length; ++i)
{
bytes.push(str.charCodeAt(i));
}
var authorization = "Basic " + base64.encode(bytes);
The "base64" package is "dojox/encoding/base64".
When sending JSON data in the payload of a REST request, set the headers property to be:
{
'Content-Type': 'application/json;charset=UTF-8'
};
also send the payload as a JSON String representation using:
JSON.stringify(object);
Page 141
Kommentare zu diesen Handbüchern