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。
在React組件中,如何使用fetch( )的返回值?
喵喔喔
2018-12-20 18:15:55