JavaScript
Joomla! 6
Joomla! 5
Joomla! 4
Component
Intermediate
Make CSRF-Protected AJAX Calls with Native Joomla.request()
Sends asynchronous POST requests with automatic CSRF headers and JsonResponse parsing using Joomla native core wrapper.
media/com_example/js/actions.js
async function sendVote(endpointUrl, itemId) {
const token = Joomla.getOptions('csrf.token', '');
const body = new URLSearchParams({ id: String(itemId) });
if (token) {
body.set(token, '1');
}
const xhr = await Joomla.request({
url: endpointUrl,
method: 'POST',
data: body.toString(),
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
promise: true,
});
const json = JSON.parse(xhr.responseText);
if (json.success) {
return json.data;
}
throw new Error(json.message || 'Erro na requisição');
}
Reads the active CSRF token directly from Joomla.getOptions('csrf.token').
Technical explanation and best practices
Joomla.request() (bundled in Joomla core) supports modern async/await via promise: true and integrates directly with Joomla CSRF tokens.