简体   繁体   English

将 jquery ajax 转换为 fetch

[英]Convert jquery ajax to fetch

Does someone knows jquery and can help me convert this ajax to js fetch ?有人知道 jquery 并且可以帮助我将此 ajax 转换为 js fetch 吗? Unfortunately, I don't know jquery and I need to convert this into js fetch.不幸的是,我不知道 jquery,我需要将其转换为 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");
    }
  });
}

Appreciate every hint.欣赏每一个提示。

I was going to use async/await but since there's that odd replacing before the JSON gets parsed I've reverted to old-school fetch with "thenables".我打算使用 async/await 但由于在解析 JSON 之前有奇怪的替换,我已经恢复到使用“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); }); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM