Ban*_*ner 13 javascript fetch reactjs
我有这个功能:
getUserData() {
fetch(this.props.apiUrl + this.state.username + '?client_Id=' + this.props.clientId + '&client_secret=' + this.props.clientSecret)
.then(function(response) {
var data = response.json();
this.setState({
userData: data
});
console.log(this.state.userData);
}.bind(this))
.catch(function(error) {
this.setState({
username: null
});
console.log(error)
}.bind(this))
}
Run Code Online (Sandbox Code Playgroud)
这在控制台中返回:
承诺{[[PromiseStatus]]:"待定",[[PromiseValue]]:undefined}
proto [[PromiseStatus]]:"已解决"
[[PromiseValue]] : Object
avatar_url : "https://avatars.githubusercontent.com/u/"
login : "hello world"
.
.
.
Run Code Online (Sandbox Code Playgroud)
我需要访问对象中的名称/值对,但我无法访问它们.我假设我需要在将响应转换为json之后再采取一步,但无法弄明白.如果有人可以提供帮助,将不胜感激!
zer*_*kms 14
response.json() 返回一个promise,所以你还需要适当地处理它,例如:
.then(function(response) {
return response.json();
})
.then(function(parsedData) {
// data here
})
Run Code Online (Sandbox Code Playgroud)
这是处理json对象的更短方法
fetch(endpoint, request)
.then(response => response.json())
.then(json => {
//handle json response
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13483 次 |
| 最近记录: |