From 9f07f1af308ce3faf3a0023d405956de4f4abf24 Mon Sep 17 00:00:00 2001 From: Dominik Heidler Date: Fri, 13 Sep 2024 14:12:02 +0200 Subject: [PATCH] Replace $.ajax() with fetch() Ticket: https://progress.opensuse.org/issues/166310 Co-authored-by: Martchus --- assets/javascripts/openqa.js | 10 ++++++++++ assets/javascripts/tests.js | 21 +++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/assets/javascripts/openqa.js b/assets/javascripts/openqa.js index dd4de2be52c9..ff505a4d2bb1 100644 --- a/assets/javascripts/openqa.js +++ b/assets/javascripts/openqa.js @@ -23,6 +23,16 @@ function setupForAll() { }); } +function getCSRFToken() { + return document.querySelector('meta[name="csrf-token"]').content; +} + +function fetchWithCSRF(resource, options) { + options.headers ??= {}; + options.headers['X-CSRF-TOKEN'] ??= getCSRFToken(); + return fetch(resource, options); +} + function makeFlashElement(text) { return typeof text === 'string' ? '' + text + '' : text; } diff --git a/assets/javascripts/tests.js b/assets/javascripts/tests.js index 724572533150..ab345cc91ac0 100644 --- a/assets/javascripts/tests.js +++ b/assets/javascripts/tests.js @@ -191,16 +191,17 @@ function changeJobPrio(jobId, delta, linkElement) { } var newPrio = currentPrio + delta; - $.ajax({ - url: urlWithBase('/api/v1/jobs/' + jobId + '/prio?prio=' + newPrio), - method: 'POST', - success: function (result) { - prioValueElement.text(newPrio); - }, - error: function (xhr, ajaxOptions, thrownError) { - addFlash('danger', 'Unable to set the priority value of job ' + jobId + '.'); - } - }); + + fetchWithCSRF(urlWithBase(`/api/v1/jobs/${jobId}/prio?prio=${newPrio}`), {method: 'POST'}) + .then(response => { + if (response.status == 200) prioValueElement.text(newPrio); + else + addFlash('danger', + `Unable to set the priority value of job ${jobId}: Server returned ${response.status}: ${response.statusText}`); + }) + .catch(error => { + addFlash('danger', `Unable to set the priority value of job ${jobId}: ${error}`); + }); } function renderTestSummary(data) {