2017-06-20 68 views
0

我不能够解析数据从获取调用从获取文本反应本地fetch调用

下面就是该方法

onLoginPress=()=>{ 
     console.log("username="+this.state.username); 
     console.log("password="+this.state.password); 
     this.sendLoginRequest(this.state.username,this.state.password) 
     .then((response) => { 
      console.log("RESPONSEEEEEEEEEEEEEEEE"); 
      console.log(response.text()) 
      console.log(Promise.resolve(response)); 
      response.json(); 
     }) 
     .then((responseJson) => { 

     console.log(responseJson); 
     }) 
     .catch((error) => { 
     console.error(error); 
     }); 


    }; 

我得到它是一个承诺,我的反应无法从中获得令牌。

下面是日志response.text()

{ _45:0,_81:1, _65: ' “3h8112qe2qobox3675ghmq9dtcbjvddc”', _54:空}

对于的console.log (Promise.resolve(response))输出为

{ 
_45: 0, 
_81: 1, 
_65: { type: 'default', 
    status: 200, 
    ok: true, 
    statusText: undefined, 
headers: { map: { connection: [ 'Keep-Alive' ], 
'content-length': [ '34' ], 
'content-type': [ 'application/json; charset=utf-8' ], 
'set-cookie': [ 'persistent_shopping_cart=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/' ], 
'cache-control': [ 'no-store, no-cache, must-revalidate' ], 
expires: [ 'Thu, 19 Nov 1981 08:52:00 GMT' ], 
pragma: [ 'no-cache' ], 
server: [ 'Apache/2.4.23 (Ubuntu)' ], 
'keep-alive': [ 'timeout=5, max=100' ], 
[ 'Tue, 20 Jun 2017 06:58:16 GMT' ] } }, 
url:'http://integration/customer/token', 
_bodyInit: '"3h8112qe2qobox3675ghmq9dtcbjvddc"', 
_bodyText: '"3h8112qe2qobox3675ghmq9dtcbjvddc"', 
bodyUsed: true 
}, 
_54: null } 

responseJson返回undefined。

如何获取令牌(3h8112qe2qobox3675ghmq9dtcbjvddc)中的数据。

谢谢!

回答

1

看来您的API正在返回文本。所以,你需要调用text()方法,并将其与then返回链:

onLoginPress=()=>{ 
     this.sendLoginRequest(this.state.username,this.state.password) 
     .then((response) => { 
     return response.text(); 
     }) 
     .then((responseJson) => { 
     console.log(responseJson); 
     }) 
     .catch((error) => { 
     console.error(error); 
     }); 
    }; 

如果你的API返回JSON,你会做,通过一个json()电话交换的text()电话一模一样。请参阅React Native fetch doc

+0

谢谢。我工作。 –