亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在React組件中,如何使用fetch( )的返回值?

在React組件中,如何使用fetch( )的返回值?

喵喔喔 2018-12-20 18:15:55
React通過fecth異步加載數據,但是fecth之后,如何才能讓我使用返回的值呢?  getInitialState:function(){    return{      result:''    }  },  proceedSubmit:function(userID,userPWD){    var res = 'Not Set';    fetch('http://127.0.0.1:80/csu/Search.php',{      method:'POST',      mode:'cors',      headers:{        'Content-Type': 'application/x-www-form-urlencoded'      },      body:'userID='+userID+'&userPWD='+userPWD    }).then(function(response){      return response.text().then(function(text){        res = text;        console.log(res);      });    }).then(function(){      console.log(res);    });    console.log(res);  },像這樣跨域fecth之后,得到了返回值text,第一二個輸出確實是返回的值,但是第三個卻是初始的“Not Set”。我該怎么樣才能使用fetch回來的值呢?讓它改變我的state。
查看完整描述

1 回答

?
蝴蝶刀刀

TA貢獻1801條經驗 獲得超8個贊

proceedSubmit: function(userID, userPWD) {

    var res = 'Not Set';

    var _this = this;           // <-------

    fetch(....).then(function(response) {

        _this.someData = text;  // <-------

    })

}


查看完整回答
反對 回復 2019-01-15
  • 1 回答
  • 0 關注
  • 1566 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號