你好代码向导,如何在JavaScript中返回通过多链接功能的对象
我想从楼下指定的网址,以获取JSON数据和在中的GetWeather之外的阵营组件使用它()函数。从内到外传递一个对象的有效方法是什么?我的控制台读取未定义。我相信有一个变量范围的问题......感谢一个想法来解决它。
下面是代码
function getWeather() {
var url = 'http://api.openweathermap.org/data/2.5/weather?zip=3000,au';
var apiKey= "005fa98ae858a29acf836ecdefac0411";
var httpRequest;
makeRequest();
var response;
function makeRequest() {
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = responseMethod;
httpRequest.open('GET', url + '&appid=' + apiKey);
httpRequest.send();
}
function responseMethod() {
if(httpRequest.readyState === 4) {
if(httpRequest.status === 200) {
updateUISucess(httpRequest.responseText);
} else {
}
}
}
function updateUISucess(responseText) {
return response = JSON.parse(responseText);
// I would like to get this JSON object out so that it logs on the console.
}
return response;
};
console.log(getWeather());
为什么不能你可以的GetWeather和它的状态存储在什么反应呢? –
这就是我想要做的。但是,我不知道如何首先获取数据并将其传递给getInitialState。 – Hooey
既然你是一个异步调用,我会尝试'react-promise'。 https://www.npmjs.com/package/react-promise –