¿Alguien sabe jquery y puede ayudarme a convertir este ajax a js fetch? Desafortunadamente, no conozco jquery y necesito convertir esto en js fetch.
function ajaxLogoutDetailsApi() { $.ajax({ type: "GET", url: "OIDCGetLogoutDetails", async: false, cache: false, data: "json", success: function (data, status, xhr) { data = data.replace('\/\*', ''); data = data.replace('\*\/', ''); var dataJson = JSON.parse(data); if (dataJson.logoutUrl != null) { document.location.href = dataJson.logoutUrl; } }, error: function (xhr, status, err) { console.log("error in ajaxLogoutDetailsApi"); } }); }Aprecia cada pista.
Iba a usar async/await, pero dado que hay un reemplazo extraño antes de que se analice el JSON, volví a la búsqueda de la vieja escuela con "thenables".
function ajaxLogoutDetailsApi() { const endpoint = 'OIDCGetLogoutDetails'; // Fetch the JSON fetch(endpoint) // This is returned as the first argument // of "then" .then(json => { // Weird replacement stuff const updated = json .replace('\/\*', '') .replace('\*\/', ''); // Parse the JSON const data = JSON.parse(updated); // Set the new page if the condition is met if (data.logoutUrl) { window.location.href = data.logoutUrl; } }) // Catch any errors .catch(error => { console.error('Error:', error); }); }