The Request Javascript module provides a mechanism for an add-on rendered in an iframe to make an XMLHttpRequest to the host product without requiring CORS.
In contrast to REST calls made from the add-on server to the product directly, any requests made in the browser are evaluated in the context of the currently logged in user. The requested resource is still evaluated against the add-ons granted scopes.
1 2 3 4 5
AP.request('/rest/api/latest/...', {
success: function(responseText){
alert(responseText);
}
});
allows for dynamic rejection of ajax requests before they can be invoked. eg: by checking against a whitelist
Execute an XMLHttpRequest as a Promise, or via callbacks, in the context of the host application. The format of the response (dataType) will be set to "text" as default; however, if binary data is requested, it will be set to "arraybuffer".
Name | Type | Description | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
url | String | Either the URI to request or an options object (as below) containing at least a 'url' property; | ||||||||||||||||||||||||||||||||||||||||||||
options | Object | The options of the request. Properties
|
1 2 3 4 5 6 7 8 9 10 11 12
// A simple POST request which logs response in the console.
AP.request({
url: '/rest/api/latest/...',
type: 'POST',
data: {name: 'some text', description: 'test'},
success: function(responseText){
console.log(responseText);
},
error: function(xhr, statusText, errorThrown){
console.log(arguments);
}
});
1 2 3 4 5 6 7 8 9 10 11 12
// Upload an attachment to a Confluence entity.
var fileToUpload = document.getElementById("fileInput").files[0];
AP.request({
url: '/rest/api/content/123456/child/attachment',
type: 'POST',
contentType: 'multipart/form-data',
data: {comment: 'example comment', file: fileToUpload},
success: function(responseText){
alert(responseText);
}
});
1 2 3 4 5
// Get the current user info using a Promise
AP.request('/rest/api/user/current')
.then(data => alert(data.body))
.catch(e => alert(e.err));
Rate this page: