2016-09-21 31 views
0

我想从一个API后的数据与此代码获得从获取数据做出反应

componentWillMount() { 
     fetch('http://url', { 
     method: 'POST', 
     body: JSON.stringify({ 
      usuario: 'conosur2', 
      password: 'test', 
     }) 

     }) 
     .then((response) => { 
     console.log(response.json()); 
    }) 

,并即时得到这个控制台

Promise {[[PromiseStatus]]: "pending", [[PromiseValue]]: undefined} 

enter image description here

我需要要访问的数据,如何?,我的意思是,像response.json('clientId')response.json().clientId(),IM即时noob与反应,所以我不知道承诺如何工作或如何获取作品(我来自aj​​ax api消费)对不起,我的英文

回答

1

response.json()返回一个承诺,所以你需要解决它

componentWillMount() { 
    fetch('http://url', { 
    method: 'POST', 
    body: JSON.stringify({ 
     usuario: 'conosur2', 
     password: 'test', 
    }) 

    }) 
    .then(function(response) { 
    return response.json() 
    }).then(function(json) { 
    console.log('parsed json', json) 
    })