• <legend id='Dqbmi'><style id='Dqbmi'><dir id='Dqbmi'><q id='Dqbmi'></q></dir></style></legend>
      <i id='Dqbmi'><tr id='Dqbmi'><dt id='Dqbmi'><q id='Dqbmi'><span id='Dqbmi'><b id='Dqbmi'><form id='Dqbmi'><ins id='Dqbmi'></ins><ul id='Dqbmi'></ul><sub id='Dqbmi'></sub></form><legend id='Dqbmi'></legend><bdo id='Dqbmi'><pre id='Dqbmi'><center id='Dqbmi'></center></pre></bdo></b><th id='Dqbmi'></th></span></q></dt></tr></i><div id='Dqbmi'><tfoot id='Dqbmi'></tfoot><dl id='Dqbmi'><fieldset id='Dqbmi'></fieldset></dl></div>
        <bdo id='Dqbmi'></bdo><ul id='Dqbmi'></ul>
      <tfoot id='Dqbmi'></tfoot>
    1. <small id='Dqbmi'></small><noframes id='Dqbmi'>

        如何从 fetch javascript 请求的响应中获取数据

        时间:2023-10-02

          <tbody id='N0QzE'></tbody>
      1. <tfoot id='N0QzE'></tfoot>

          1. <i id='N0QzE'><tr id='N0QzE'><dt id='N0QzE'><q id='N0QzE'><span id='N0QzE'><b id='N0QzE'><form id='N0QzE'><ins id='N0QzE'></ins><ul id='N0QzE'></ul><sub id='N0QzE'></sub></form><legend id='N0QzE'></legend><bdo id='N0QzE'><pre id='N0QzE'><center id='N0QzE'></center></pre></bdo></b><th id='N0QzE'></th></span></q></dt></tr></i><div id='N0QzE'><tfoot id='N0QzE'></tfoot><dl id='N0QzE'><fieldset id='N0QzE'></fieldset></dl></div>
            • <bdo id='N0QzE'></bdo><ul id='N0QzE'></ul>
              <legend id='N0QzE'><style id='N0QzE'><dir id='N0QzE'><q id='N0QzE'></q></dir></style></legend>

                <small id='N0QzE'></small><noframes id='N0QzE'>

                • 本文介绍了如何从 fetch javascript 请求的响应中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I make fetch request in javascript . It is fetching data when I seed console it show me data. but when I try to alert it in then function it displays empty. it show alert promptly with page load. I think it alerting before request response

                  Here is my javascript code

                  fetch("https://01b4e41e6262.ngrok.io/api/get_schedule_orders/" + gUser.getData().id).then(res => {
                          if (res.ok) {
                          alert(res)
                          }
                      });
                  

                  解决方案

                  fetch() returns a Promise initially, so res is initially a promise, either resolved or rejected. Then res.json() again returns a promise and not the value directly (You may verify this by doing a console.log(res) in the first then(), there in the prototype you will see json(), which is again Promise based.

                  That's why we chain promises by doing return res.json() and get our data in the second promise resolve and in case of rejection catch() callback is invoked.

                  fetch("https://01b4e41e6262.ngrok.io/api/get_schedule_orders/" + gUser.getData().id).then(res => {
                      if (res.status>=200 && res.status <300) {
                        return res.json()
                      }else{
                        throw new Error();
                      }
                  }).then(data=>console.log(data))
                   .catch(err=>console.log('fetch() failed'))
                  

                  UPDATE: your API is returning an empty array.

                  Please check the API params.

                  这篇关于如何从 fetch javascript 请求的响应中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:有没有办法强制 XMLHttpRequest 使用 HTTP/1.1? 下一篇:whatwg-fetch 新的打字稿 2.5.3

                  相关文章

                  <legend id='zijWs'><style id='zijWs'><dir id='zijWs'><q id='zijWs'></q></dir></style></legend>
                • <tfoot id='zijWs'></tfoot>
                  • <bdo id='zijWs'></bdo><ul id='zijWs'></ul>

                  1. <i id='zijWs'><tr id='zijWs'><dt id='zijWs'><q id='zijWs'><span id='zijWs'><b id='zijWs'><form id='zijWs'><ins id='zijWs'></ins><ul id='zijWs'></ul><sub id='zijWs'></sub></form><legend id='zijWs'></legend><bdo id='zijWs'><pre id='zijWs'><center id='zijWs'></center></pre></bdo></b><th id='zijWs'></th></span></q></dt></tr></i><div id='zijWs'><tfoot id='zijWs'></tfoot><dl id='zijWs'><fieldset id='zijWs'></fieldset></dl></div>

                      <small id='zijWs'></small><noframes id='zijWs'>