从本机获取调用中获取文本

getting text from react native fetch call

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

方法如下

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(响应))输出是

{ 
_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 returns 未定义。

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

谢谢!

您的 API 似乎是 returning 文本。所以你需要调用 text() 方法并 return 它与 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 return 和 JSON,您可以通过将 text() 调用换成 json() 调用来做完全相同的事情。请参阅 React Native fetch doc.