Rolf
May 12, 2022, 8:22am
1
How can I make an AJAX or API call in an addon, i.e. via javascript (cockpit frontend), e.g. if I have a chart and generate the data in the backend and prepare it as JSON?
Jamo
May 12, 2022, 11:55am
2
Hi @Rolf
You want to get data from Cockpit in your frontend, right?
If that’s the case, please refer to the docs .
Let me know if this helped.
Rolf
May 12, 2022, 12:07pm
3
not in my frontend. in the fronted of cockpit.
the ajax-call must call on a part of the code from addon. and the fontend part of same plugin must call over javascript on this same area of plugin.
@Rolf Have a look at the core modules and App.request()
.
In short:
$app->bind('/mydata', function() {
return ['key' => 'value'];
});
App.request('/mydata').then(function(data) {
console.log(data);
});
References:
null, null,
App.route(['/collections/entries/', this.collection.name, '?q=', JSON.stringify({
page: this.page || null,
filter: this.filter || null,
sort: this.sort || null,
limit: this.limit
})].join(''))
);
}
return App.request('/collections/find', {collection:this.collection.name, options:options}).then(function(data){
window.scrollTo(0, 0);
this.entries = data.entries;
this.pages = data.pages;
this.page = data.page;
this.count = data.count;
this.checkselected();
this.loading = false;
'parent_id_column_name' => '_pid',
'children_key_name' => 'children',
'id_column_name' => '_id',
'sort_column_name' => '_o'
]);
}
return $items;
}
public function find() {
\session_write_close();
$collection = $this->app->param('collection');
$options = $this->app->param('options');
if (!$collection) return false;
$collection = $this->app->module('collections')->collection($collection);
},
route: function(url) {
return this.base_route+url;
},
reroute: function(url){
location.href = url.match(/^http/) ? url : this.route(url);
},
request: function(url, data, type) {
url = this.route(url);
type = type || 'json';
return new Promise(function (fulfill, reject){
var xhr = new XMLHttpRequest();
xhr.open('post', url, true);
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');