简体   繁体   English

如何将此 Jquery Ajax 转换为纯 Javascript Fetch?

[英]How to convert this Jquery Ajax to Pure Javascript Fetch?

I wanna convert this snippet from Jquery Ajax to Fetch Pure Javascript.我想将此片段从 Jquery Ajax 转换为 Fetch Pure Javascript。 Can help me please?可以帮帮我吗?

I tried this before, but my code doesn't work as it should.我以前试过这个,但我的代码不能正常工作。 I even asked the question here .我什至在这里问了这个问题。

That's why I would like you to help me to convert it from zero, I could not.这就是为什么我希望你帮我把它从零转换,我做不到。

ajaxCall = $.ajax({
  url: "data.php",
  dataType: "json",
  cache: false,
  type: "POST",
  data: "ajax=1&do=check&lista=" + encodeURIComponent(leray[chenille]),
  success: function (oreen) {
    switch (oreen.enviando) {
      case -1:
        chenille++;
        $("#div1").append(oreen.cat + "<br />");
        updateProgress(chenille, leray.length);
        tvmit_wrongUp();
        break;

      case 1:
        chenille++;
        $("#div1").append(oreen.dog + "<br />");
        updateProgress(chenille, leray.length);
        tvmit_wrongUp();
        break;

      case 2:
        chenille++;
        $("#div2").append(oreen.sky + "<br />");
        nieva++;
        updateProgress(chenille, leray.length);
        tvmit_dieUp();
        break;

      case 3:
        chenille++;
        $("#div3").append(oreen.water + "<br />");
        tvmit_liveUp();
        updateProgress(chenille, leray.length);
        break;
    }

    OKTY(leray, chenille, aarsh, nieva);
  }
});
return true;

You can go with this.你可以用这个 go。

  1. URL is first option in the fetch method. URL 是fetch方法中的第一个选项。
  2. cache: false equivalent in fetch is cache: 'no-cache' cache: false fetch 中的等价物是cache: 'no-cache'
  3. type: POST equivalent in fetch is method: 'POST' type: POST获取中等效的 POST 是method: 'POST'
  4. You can add data as query in the URL您可以在 URL 中添加data作为查询
  5. To convert data to JSON after getting the response , then execute reponse.json()获取response后将数据转换为JSON ,然后执行reponse.json()

Lastly, you need to execute this in an async function, so you can await , or instead use then syntax.最后,您需要在async function 中执行此操作,因此您可以await ,或者使用then语法。

try {
    const response = await fetch(`data.php?ajax=1&do=check&lista=${encodeURIComponent(leray[chenille])}`, {
      method: 'POST',
      cache: 'no-cache'
    })

    const oreen = await response.json();

    switch (oreen.enviando) {
          case -1:
            chenille++;
            $("#div1").append(oreen.cat + "<br />");
            updateProgress(chenille, leray.length);
            tvmit_wrongUp();
            break;

          case 1:
            chenille++;
            $("#div1").append(oreen.dog + "<br />");
            updateProgress(chenille, leray.length);
            tvmit_wrongUp();
            break;

          case 2:
            chenille++;
            $("#div2").append(oreen.sky + "<br />");
            nieva++;
            updateProgress(chenille, leray.length);
            tvmit_dieUp();
            break;

          case 3:
            chenille++;
            $("#div3").append(oreen.water + "<br />");
            tvmit_liveUp();
            updateProgress(chenille, leray.length);
            break;
        }

        OKTY(leray, chenille, aarsh, nieva);
    }
    } catch (e) {
      console.log(e)
    }

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

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