从响应本地获取调用获取文本

Shr*_*air 2 response fetch react-native

我无法解析提取调用中的数据

下面是方法

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);
      });


    };
Run Code Online (Sandbox Code Playgroud)

我得到的回应是一个承诺,我无法摆脱它的代币。

以下是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 }
Run Code Online (Sandbox Code Playgroud)

responseJson返回未定义。

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

谢谢!

Ker*_*men 5

您的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);
      });
    };
Run Code Online (Sandbox Code Playgroud)

如果您的API返回JSON,则可以text()通过一次json()调用交换该调用来执行完全相同的操作。请参阅React Native 提取文档