2017-05-09 74 views
1

我认为问题是值仍然为空我需要设置值,但我不知道如何。我的API可以读取,但我不能输出react.js

constructor() { 
    super(); 
    this.state = { 
     jsonReturnedValue: null 
    } 
    } 
在这方面我设置了一个空值和

试图获取到我的反应,但是当我运行它只有“员工列表”可以看出

fetch("http://localhost:5118/api/employeedetails/getemployeedetails") 
    .then(handleErrors) 
    .then(response => response.json()) .then(json => { 
     this.setState({ jsonReturnedValue: response.results }); 
    }).catch(function(error) { 
     console.log(error); 
    }); 

    } 
    } 

render() { 
    return(
     <div> 
     <h1> Listof Employees </h1> 
      <h1>{ this.state.jsonReturnedValue }</h1> 
     </div> 
+0

没有获得响应你。然后,但json,console.log(json)在.then回调,然后你可以看到你想要设置为jsonReturnedValue –

回答

0

的问题是response.resultsresponse不在第三个then调用中,只有第二个。

尝试:

fetch("http://localhost:5118/api/employeedetails/getemployeedetails") 
    .then(handleErrors) 
    .then(response => response.json()) 
    .then(json => { 
     this.setState({ jsonReturnedValue: json.results }); 
    }).catch(function(error) { 
     console.log(error); 
    }); 
0

使用爱可信的HTTP请求。并签入然后承诺使用console.log(response);响应来自api或不是

0

fetch功能是否停留在您的componentDidMount()?就像这样:

componentDidMount() { 
    fetch('http://localhost:5118/api/employeedetails/getemployeedetails') 
    .then(resp => resp.json()) 
    .then((resp) => { 
    console.log(resp); 
    this.setState({jsonReturnedValue: JSON.stringify(resp)}); 
    }).catch(function(error) { 
    console.log(error); 
    }); 
} 

也请大家注意,this.setState({jsonReturnedValue: JSON.stringify(resp)}) =>的resp的字符串化版本,必须使用时,你只是想放弃它,看看。 (因为在render(),你刚回来<h1>{ this.state.jsonReturnedValue }</h1>

但是,您可能循环(可能使用.map)的jsonReturnedValue和渲染每个员工的正确,那么请删除JSON.stringify

相关问题