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

    <legend id='NEhE9'><style id='NEhE9'><dir id='NEhE9'><q id='NEhE9'></q></dir></style></legend>
      <tfoot id='NEhE9'></tfoot>

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

        • <bdo id='NEhE9'></bdo><ul id='NEhE9'></ul>
      1. 如何从 fetch API 返回 json 响应

        How to return the json response from the fetch API(如何从 fetch API 返回 json 响应)
      2. <legend id='v9oUa'><style id='v9oUa'><dir id='v9oUa'><q id='v9oUa'></q></dir></style></legend>
          <i id='v9oUa'><tr id='v9oUa'><dt id='v9oUa'><q id='v9oUa'><span id='v9oUa'><b id='v9oUa'><form id='v9oUa'><ins id='v9oUa'></ins><ul id='v9oUa'></ul><sub id='v9oUa'></sub></form><legend id='v9oUa'></legend><bdo id='v9oUa'><pre id='v9oUa'><center id='v9oUa'></center></pre></bdo></b><th id='v9oUa'></th></span></q></dt></tr></i><div id='v9oUa'><tfoot id='v9oUa'></tfoot><dl id='v9oUa'><fieldset id='v9oUa'></fieldset></dl></div>
          <tfoot id='v9oUa'></tfoot>

              <tbody id='v9oUa'></tbody>
                  <bdo id='v9oUa'></bdo><ul id='v9oUa'></ul>

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

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

                  问题描述

                  我有这样的功能:

                  check_auth(){
                      fetch(Urls.check_auth(), {
                        credentials: 'include',
                        method: 'GET'
                      }).then(response => {
                        if(response.ok) return response.json();
                      }).then(json => {
                        return json.user_logged_in;
                      });
                    }
                  

                  然后我尝试这样做:

                  if(this.check_auth()){
                      // do stuff
                  } else {
                      // do other stuff
                  }
                  

                  但是,this.check_auth() 总是 undefined.

                  我在这里缺少什么?我认为在 fetch 的 then() 中是 resolved Promise 对象的位置,因此我认为当用户登录时我会得到 true在.但事实并非如此.

                  What am I missing here? I thought that within fetch's then() was where the resolved Promise object was therefore I thought that I'd get true when the user was logged in. But this is not the case.

                  任何帮助将不胜感激.

                  推荐答案

                  使用回调

                  check_auth(callback){
                      fetch(Urls.check_auth(), {
                        credentials: 'include',
                        method: 'GET'
                      }).then(response => {
                        if(response.ok) return response.json();
                      }).then(json => {
                        callback(json.user_logged_in);
                      });
                    }
                  
                   check_auth(function(data) {
                          //processing the data
                          console.log(d);
                      });
                  

                  在 React 中它应该更容易处理,您可以调用 fetch 并更新状态,因为每次使用 setState 更新状态时都会调用渲染方法,您可以使用状态进行渲染

                  In React it should be easier to handle, You can call a fetch and update the state, since on every update of state using setState the render method is called you can use the state to render

                  check_auth = () =>{
                      fetch(Urls.check_auth(), {
                        credentials: 'include',
                        method: 'GET'
                      }).then(response => {
                        if(response.ok) return response.json();
                      }).then(json => {
                           this.setState({Result: json.user_logged_in});
                      });
                    }
                  

                  这篇关于如何从 fetch API 返回 json 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  业务场景:使用update语句去更新数据库字段。 原因:update接收值不正确。原来代码: $query = "UPDATE student SET date = now() WHERE id = $id";$result = $mysqli-query($query2) or die($mysqli-error); // 问题出现了在这句 $data = $result-fetch_ass
                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)

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

                    <tbody id='kyBh4'></tbody>

                      <legend id='kyBh4'><style id='kyBh4'><dir id='kyBh4'><q id='kyBh4'></q></dir></style></legend>

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