将jQuery ajax转换为获取

发布于 2025-02-12 16:44:15 字数 641 浏览 0 评论 0原文

有人知道jQuery,可以帮助我将此Ajax转换为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");
    }
  });
}

感谢每个提示。

Does someone knows jquery and can help me convert this ajax to js fetch ?
Unfortunately, I don't know jquery and I need to convert this into 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独自唱情﹋歌 2025-02-19 16:44:15

我本来要使用异步/等待,但是由于在解析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);
    });

}

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".

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);
    });

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文