【发布时间】:2017-04-02 15:02:52
【问题描述】:
我一直在 Stack 上寻找一段时间,但我无法找到解决方案。
我有一个调用的方法应该使用 Axios 执行 GET 请求。
在方法开始时我创建了一个新的 hash ,所以我想将所有信息存储在那里,然后返回那个 hash。
import React, {Component} from "react";
import axios from 'axios';
class Json extends Component{
getInformation(){
let inf = {};
axios
.get("http://codepen.io/jobs.json")
.then(result=> {
inf.description = result.data.jobs[0].description;
})
.catch(err=>{
console.log("STH WRONG");
});
console.log(inf);
return(inf);
}
那我的问题是什么???如果我检查 inf 变量中的内容,它是空的。但是,如果我使用 Chrome 进行检查,在控制台上我会看到它是空的,但是当我仔细检查时,键和值就在那里。目前我正在尝试使用inf.description 检索信息是未定义的。
有什么想法吗??随意尝试这个例子,只需调用这个方法来检查它。
编辑
import React, {Component} from 'react';
import axios from 'axios';
class Json extends Component{
constructor(props){
super(props);
this.state={title: ""};
}
getInformation(){
let inf = {};
// .get("/data/CA_data.json")
axios
.get("http://codepen.io/jobs.json")
.then(result=> {
inf.description = result.data.jobs[0].description;
})
.catch(err=>{
console.log("STH WRONG");
});
console.log(inf);
return(inf);
}
render(){
let inf = this.getInformation();
return(
<h1>
</h1>
);
}
}
export default Json;
【问题讨论】:
-
我会在笔记本电脑上检查它!它看起来非常有趣和有用
-
@azium Man 非常感谢这个链接。我在那里遇到了一些问题,感谢我解决了这个问题!
标签: javascript reactjs hash axios